08.
Inserting and Querying Data
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
18 Comments
Hi!
When I run the play.php in the cli, I get the following error message:
Fatal error: Class 'Yoda\EventBundle\Entity\Event' not found in C:\wamp\www\starwarsevent\play.php on line 26
Call Stack:
0.0002 243240 1. {main}() C:\wamp\www\starwarsevent\play.php:0
What can I do to fix this? I'm using Symfony 2.7 (newest).
Hey there!
Hmm, so the class - Yoda\EventBundle\Entity\Event - looks good to me, so the likely cause is the autoloader. Do you still have the original autoload require line? https://github.com/symfony/... And is your code after this? If it *is* there, post your full file on a Gist and we can figure it out :).
Cheers!
Hey, I sort of fixed it just before I received your message. I reset everything to the first commit and followed the tutorial again, and now it works!
I think I made some mistakes while trying to reset to my first commit earlier and some code may have been doubled or erased.
Anyway, thanks for your swift reply. I'm really enjoying the course so far!
Hi Ryan,
Could you please instruct us how to use Twig with Bootstrap CSS framework in Symfony2?
P.S. updated tutorials are fantastic, and thanks for the free all access. :-)
Hey Enkhbilguun!
My pleasure! If you want to use Twig and Bootstrap together, that really has (almost) nothing to do with Symfony - just include the CSS file in your base template and start using the markup. The only place where they integrate is when you're rendering forms. If you're asking about that, it *can* get quite tricky. Fortunately, Symfony 2.6 comes with a Twitter Bootstrap form theme out of the box. You can read about it here: https://github.com/symfony/.... If you are not using Symfony 2.6 (it's not even out yet), you can just copy the form theme into your project and use it as a form theme as if you had created it yourself. The source file is here: https://github.com/symfony/...
I hope that helps!
Thanks Ryan. That's exactly what I asked. :-)
I'm getting an exception and an error right after pressing enter button multiple times for parameters. I'm trying to install the latest version v2.5 with "composer create-project symfony/framework-standard-edition starwarevents @stable" command.
What could I do?
==========
Writing lock file
Generating autoload files
Would you like to install Acme demo bundle? [y/N]
Creating the "app/config/parameters.yml" file
Some parameters are missing. Please provide them.
database_driver (pdo_mysql):
database_host (127.0.0.1):
database_port (null):
database_name (symfony):
database_user (root):
database_password (null):
mailer_transport (smtp):
mailer_host (127.0.0.1):
mailer_user (null):
mailer_password (null):
locale (en):
secret (ThisTokenIsNotSoSecretChangeIt):
debug_toolbar (true):
debug_redirects (false):
use_assetic_controller (true):
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /Users/beregu/Dropbox/Sites/starwarevents/vendor/monolog/monolog/src/Monolog/Logger.php line 233
cache:clear [--no-warmup] [--no-optional-warmers]
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.
create-project [-s|--stability="..."] [--prefer-source] [--prefer-dist] [--repository-url="..."] [--dev] [--no-dev] [--no-plugins] [--no-custom-installers] [--no-scripts] [--no-progress] [--keep-vcs] [--no-install] [package] [directory] [version]
[SOLVED]
It looks my composer was using my php built-in with Mac OSX Yosemite while I was thinking it was running on MAMP.
Pointed the path to the PHP in MAMP, and the installation has been completed smoothly. For those who may experience the same, have a look at: http://laravel.io/forum/05-...
For a scalar types I don't like use
nullable=trueoption. I prefer to set a default value on property definition and then in setter I use type casting to correct scalar type or add simple check likeisset($description) ? $description : 'Some default value'. Please, check my entity example wheredescriptionandpriceproperties are optional:Ryan, what do you think about this way?
Hey!
I like it just fine! I don't usually type-hint, because Doctrine will eventually cast (for example) a string to an int. But technically, this is more strict - since your object will *always* (for example) have an int for a price, instead of a string. I'll probably start doing this more when scalar type-casting is available. But I like it just fine :).
About nullable=false, I use this specifically if I want to guarantee that I don't mess something up and forget to set a field. For example, I might set nullable=false on price, because I *want* there to be a huge exception if I somehow create a Product and forget to set this fields - i.e. having a real value here is so important, the site doesn't really work without explicitly setting it. So I think both methods are valid: nullable = false to be crazy strict, and using default values when having a default value makes sense.
What do you think?
Thanks for your reply!
The reason to avoid `nullable=true` for scalar types nice uncovered in http://stackoverflow.com/a/... answer. I think `nullable=true` mainly should use for foreign keys, however in some cases it could applied for scalar types, but such cases not much.
Also, I'm looking forward to a PHP7 stable release with its scalar type hinting and super performance :)
Hi guys, thanks so much for the super helpful tutorials. Unfortunately I've hit an odd wall with a PDOException: "could not find driver", presumably for pdo_mysql. Ive confirmed that it is installed/enabled. I can access tables with php app/console doctrine:query:sql, and anything I run from the play.php script works fine. When I move on to running it in the browser, I get this driver error. I loaded phpinfo on a page and produced a path to my cli/php.ini. Double checked that and everything seems to be ok there. Any other thoughts? Thanks for your help.
Hi Alex!
First, I think you're probably right about the missing pdo_mysql driver. This sounds like a classic case of having 2 PHP's installed on the system - the one from the command line has pdo_mysql installed, but the one used by your web server is using a different PHP that does not.
Are you using the built-in PHP web server? If you are, then I'm wrong :). But if you aren't, let's try it. So, from the root of your project:
php app/console server:run
Then go to http://localhost:8000 in your browser. This will run your site, but use your cli PHP, which we're pretty sure has pdo_mysql installed. If this page loads, then the problem is with a different PHP being used by your web server. If you still get the error, then something else is going on.
Let me know!
The autocompletion for ->getManager is not working in my phpstorm IDE though the code is executed without problems - am I missing something? Thanks!
Hey Max!
Hmm. Assuming you're on Symfony 3, make sure you right click on the var/cache directory and "Mark Directory As" -> "Excluded". We do it at about the 2:00 mark in this video: http://knpuniversity.com/sc...
Lemme know if that works! The cache directory holds some duplicate Symfony files (for performance reasons) but this can confuse PhpStorm. If that is *not* the problem/fix... lemme know!
Cheers!
Nope, I'm running symfony 2.8 and sadly there is no var/cache directory...
$em=$this->getDoctrine()->getManager(); works fine and I get autocompletion for every step.
$em2=$this->container->get('doctrine')->getManager(); stops the autocompletion after get('doctrine').
Hey Max!
Ah, then 2 things:
1) I repeat my advice from earlier about "Excluding" the directory - but instead, include app/cache. Symfony had app/cache in 2.x and var/cache in 3.x (they're the same thing, we just changed the dir name).
2) Do you have the Symfony plugin installed (*and* enabled for this project)? https://knpuniversity.com/s... Also make sure you have the "PHP Annotations" plugin
Lemme know if either of these help :)
You are a problem-solving-monster :D I had the plugin installed, though not activated... now everything runs smoothly!
"Houston: no signs of life"
Start the conversation!