When you just start to type word ( action ) you get choice ( action ) and then make auto complete of function and @Rote annotation above, two in one. How do you manage this?)) When I type ( action ) I have only one choice ( abstract ). There is no ( action ) at all. ((
Yes! It's incredibly powerful with Symfony and Annotations plugins! For more basic/easy stuff SublimeText is still my favorite but, when you start working hardly with complex architectures and git, PhpStorm is indeed the Best IDE ever! :) Awesome namespaces feature.
Nice catch! This was included in the code block, but the `use` line wasn't being displayed by default. I'm updating the code block right now to show it :).
Do you need getters and setters for Doctrine/Symfony to work? Or can you just make the Entity properties public?
Also... along the same lines... why even use getters and setters? I've read that its so that you can later add further functionality to do more in the "get" or "set" function if you need to, but if you did need to inject that extra functionality later, couldn't you just set the variable equal to an anonymous function or a closure? (or is that not allowed in a php property?)
You can absolutely just make the properties public. And you're also right about why we do this (i.e. needing to tweak how it works later). And no, there's no way to do this later. If you make the public property, then all of your code will look like $myObj->name = 'foo'. But then later if you need to add some logic, your only option is to add a setter, which means also needing to update your code everywhere to look like $myObj->setName('foo').
It is a bit of a shortcoming imo with PHP. Other languages (and this is still be discussed for PHP, I believe) allow you to just have the property, but use the getter/setter syntax (so that later, if you do need logic, THEN you can add the getter/setter without needing to change any other code).
EDIT: found the solution, i had to use app_dev.php in my URL.
While using your code in my newAction() method, and going to ../genus/new, I get the same page as showAction but with "NEW" instead of the genus name. It probably routes to showAction's @Route("/genus/{genusName}"). The $genus is also not persisted in my db (I'm working with MAMP pro and using the php server).
Here's my code:
class GenusController extends Controller { /** * @Route("/genus/new") */ public function newAction() { $genus = new Genus(); $genus->setName('Octopus'.rand(1, 100)); $em = $this->getDoctrine()->getManager(); $em->persist($genus); $em->flush(); return new Response('<html><body>Genus created!</body></html>'); }
/** * @Route("/genus/{genusName}") */ public function showAction($genusName) { //... }
If you are not using the built-in server as shown in the tutorial, you need to specify *app_dev.php* in your paths like this: http://localhost/app_dev.php/genus/new So you can hit the dev environment, otherwise you would be hitting app.php and that's the file for production environment
You can check the .htaccess in the web directory so you can really see why is this happening.
Heh. Was about to ask "Why doesnt http://localhost:8000/app.php/genus/new" work. Of course because I had to clear the cache so that the new routes would work.
One Q : why is Doctrine so "obvious" at the newAction level ($this->getDoctrin()->...) ? Do we discover later how to abstract it yet further where our entire controller could just change from Doctrine to something different?
Well, Doctrine is already an abstraction layer for low level database drivers: MySQL, PgSQL, Sqlite, etc. - see the http://docs.doctrine-projec... . So switching from MySQL to PgSQL will be pretty easy, since you have to tweak some config values and DB queries and that's it. But if you decide to get rid of Doctrine one day - probably you have to do much more work :) Actually, Doctrine is defacto standard in Symfony applications, so doing more layers is pointless, especially for education purposes - it involves more complex code. And fair enough, this tutorial about Doctrine, so we explains only Doctrine examples in it ;)
Anyway, if you want to consider something different from Doctrine in the future - you will probably have to do a lot of changes: implement other repositories, tweak ALL your queries, since you won't have Doctrine Query Builder, etc. So specifying Doctrine so "obvious" in actions is a too minor in comparison with it.
Hello guys! Everything is working perfect so far (some changes because of Symfony version - I'm using 3.3.6, but is fine) but now, as soon as I inserted the data into Genus table, the debug toolbar doesn't show up. Any ideas? Thanks [ SOLVED ] Forget it, my fault!
I can insert my data. I checked it via mysql workbench and i could see the data ^^. But when try this : php bin/console doctrine:query:sql 'SELECT * FROM genus';
this sweet exception said hi to me;
[Symfony\Component\Console\Exception\RuntimeException] Too many arguments.
Hmm - try removing the ; at the end - that doesn't need to be there (or, it should be inside the quotes at least!). Also, if that doesn't work, try double-quotes. You should be able to wrap the argument with single or double quotes, but just in case, try it - if you happen to be using Windows, sometimes weird things happen. Basically, this error is saying that the doctrine:query:sql command takes a single argument and you are giving it more than 1 argument. Arguments are separated by spaces, so it appears to think that you are passing it 2 or more arguments. It could just be that extra ; :)
Yes, you're totally correct! Symfony parses only doc-block comments, it's a feature of PHP, so you should start all your annotations with "/**". Thanks for this tip!
Anyone else run into this issue when trying to create the table?
"An exception occured in driver: could not find driver 500 Internal Server Error - DriverException 2 linked Exceptions: PDOException » PDOException »"
Everything worked ok when creating the database itself. The database is remote, but creating it from my local machine worked out ok (though I did get a similar error my first time through until I uncommented a line in php.ini)
I suppose you're trying to use MySQL DB, so It looks like you don't have php5-pdo_mysql (or php70-pdo_mysql) PHP extension. You can easily install it with `apt-get` package manager on Ubuntu/Debian machine. If it doesn't help, or it's already installed, then please, double check your php.ini configuration and ensure you include mysql extension
Awesome. Thanks for the reply. How do I install that on a Windows machine? (I typically work on Ubuntu, but this is my gaming/hobby computer I've been doing this on).
[Edit] PS just checked. My php.ini has this line of code uncommented
extension=php_pdo_mysql.dll
and I have a file called php_pdo_mysql.dll in my ext folder in php\ext
Am I maybe missing some kind of screwy windows configuration setting or something?
Ah, it difficult to say for windows - it depends on how did you install PHP on it. I doesn't work on Windows, so I think you just need to google "How to install pdo_mysql PHP %your_version_here% extension for Windows".
I got same issue, and resolve it using this step: 1.) Installing pdo_mysql: apt-get install php7.0-pdo-mysql 2.) Write to symfony homepage phpinfo(); Then in browser see something like: "Loaded Configuration File: /etc/php7.0/cli/php.ini". 3.) sudo vi /etc/php7.0/cli/php.ini and uncommented "extension=php_pdo_mysql.dll" 4.) Restart PHP 5.) Restart Symfony process
Sorry for my english, just I recently started learning
35 Comments
When you just start to type word ( action ) you get choice ( action ) and then make auto complete of function and @Rote annotation above, two in one. How do you manage this?))
When I type ( action ) I have only one choice ( abstract ). There is no ( action ) at all. ((
Hey Andrew! Here's the magic: https://knpuniversity.com/b... I have a live template for "action" - very handy!
Oh yeah it shreds !
great, thanks.
I was wondering the same :) thought it was some kind of problem with PhpStorm and Annotations plugin.
We all love PhpStorm :)
Yes! It's incredibly powerful with Symfony and Annotations plugins!
For more basic/easy stuff SublimeText is still my favorite but, when you start working hardly with complex architectures and git, PhpStorm is indeed the Best IDE ever! :) Awesome namespaces feature.
It is not mentioned to add " use AppBundle\Entity\Genus; " in the Controller. Without this an exception occurs.
Nice catch! This was included in the code block, but the `use` line wasn't being displayed by default. I'm updating the code block right now to show it :).
Thanks!
Do you need getters and setters for Doctrine/Symfony to work? Or can you just make the Entity properties public?
Also... along the same lines... why even use getters and setters? I've read that its so that you can later add further functionality to do more in the "get" or "set" function if you need to, but if you did need to inject that extra functionality later, couldn't you just set the variable equal to an anonymous function or a closure? (or is that not allowed in a php property?)
Yo Terry!
You can absolutely just make the properties public. And you're also right about why we do this (i.e. needing to tweak how it works later). And no, there's no way to do this later. If you make the public property, then all of your code will look like
$myObj->name = 'foo'. But then later if you need to add some logic, your only option is to add a setter, which means also needing to update your code everywhere to look like$myObj->setName('foo').It is a bit of a shortcoming imo with PHP. Other languages (and this is still be discussed for PHP, I believe) allow you to just have the property, but use the getter/setter syntax (so that later, if you do need logic, THEN you can add the getter/setter without needing to change any other code).
Cheers!
In php, later, you always can to make field private and add your business logic to __set/__get magic.
EDIT: found the solution, i had to use app_dev.php in my URL.
While using your code in my newAction() method, and going to ../genus/new, I get the same page as showAction but with "NEW" instead of the genus name. It probably routes to showAction's @Route("/genus/{genusName}"). The $genus is also not persisted in my db (I'm working with MAMP pro and using the php server).
Here's my code:
class GenusController extends Controller
{
/**
* @Route("/genus/new")
*/
public function newAction()
{
$genus = new Genus();
$genus->setName('Octopus'.rand(1, 100));
$em = $this->getDoctrine()->getManager();
$em->persist($genus);
$em->flush();
return new Response('<html><body>Genus created!</body></html>');
}
/**
* @Route("/genus/{genusName}")
*/
public function showAction($genusName)
{
//...
}
Hey Helene Shaikh!
If you are not using the built-in server as shown in the tutorial, you need to specify *app_dev.php* in your paths like this: http://localhost/app_dev.php/genus/new
So you can hit the dev environment, otherwise you would be hitting app.php and that's the file for production environment
You can check the .htaccess in the web directory so you can really see why is this happening.
Have a nice day!
Heh. Was about to ask "Why doesnt http://localhost:8000/app.php/genus/new" work. Of course because I had to clear the cache so that the new routes would work.
Hey Richie,
Yeah, you're right! ;) you have to clear the prod cache each time you change configuration files, templates, etc.
Cheers!
One Q : why is Doctrine so "obvious" at the newAction level ($this->getDoctrin()->...) ? Do we discover later how to abstract it yet further where our entire controller could just change from Doctrine to something different?
Hey Richie,
Well, Doctrine is already an abstraction layer for low level database drivers: MySQL, PgSQL, Sqlite, etc. - see the http://docs.doctrine-projec... . So switching from MySQL to PgSQL will be pretty easy, since you have to tweak some config values and DB queries and that's it. But if you decide to get rid of Doctrine one day - probably you have to do much more work :) Actually, Doctrine is defacto standard in Symfony applications, so doing more layers is pointless, especially for education purposes - it involves more complex code. And fair enough, this tutorial about Doctrine, so we explains only Doctrine examples in it ;)
Anyway, if you want to consider something different from Doctrine in the future - you will probably have to do a lot of changes: implement other repositories, tweak ALL your queries, since you won't have Doctrine Query Builder, etc. So specifying Doctrine so "obvious" in actions is a too minor in comparison with it.
Cheers!
Hello guys!
Everything is working perfect so far (some changes because of Symfony version - I'm using 3.3.6, but is fine) but now, as soon as I inserted the data into Genus table, the debug toolbar doesn't show up.
Any ideas?
Thanks
[ SOLVED ]
Forget it, my fault!
Hey Mauricio Marini!
We are glad to hear you could fix your problem by yourself :)
Have a nice day
I can insert my data. I checked it via mysql workbench and i could see the data ^^.
But when try this : php bin/console doctrine:query:sql 'SELECT * FROM genus';
this sweet exception said hi to me;
[Symfony\Component\Console\Exception\RuntimeException]
Too many arguments.
Hi there!
Hmm - try removing the ; at the end - that doesn't need to be there (or, it should be inside the quotes at least!). Also, if that doesn't work, try double-quotes. You should be able to wrap the argument with single or double quotes, but just in case, try it - if you happen to be using Windows, sometimes weird things happen. Basically, this error is saying that the doctrine:query:sql command takes a single argument and you are giving it more than 1 argument. Arguments are separated by spaces, so it appears to think that you are passing it 2 or more arguments. It could just be that extra ; :)
Cheers!
hi
yes, I am on windows and double quotes work for me (too) :)
Thanks a lot
If anyone has issues with the new route not showing up after clearing cache, my problem was that I started the annotation with a single *
symfony only seems to pick it up if you use 2 **
Hey Robert,
Yes, you're totally correct! Symfony parses only doc-block comments, it's a feature of PHP, so you should start all your annotations with "/**". Thanks for this tip!
Cheers!
For query, single quotes doesn't work for me (I'm on windows). But it's ok with double quotes.
Ah, fascinating - I didn't know Windows didn't like the single quotes. Thanks for sharing!
Anyone else run into this issue when trying to create the table?
"An exception occured in driver: could not find driver
500 Internal Server Error - DriverException
2 linked Exceptions: PDOException » PDOException »"
Everything worked ok when creating the database itself. The database is remote, but creating it from my local machine worked out ok (though I did get a similar error my first time through until I uncommented a line in php.ini)
Hey somecallmetim ,
I suppose you're trying to use MySQL DB, so It looks like you don't have php5-pdo_mysql (or php70-pdo_mysql) PHP extension. You can easily install it with `apt-get` package manager on Ubuntu/Debian machine. If it doesn't help, or it's already installed, then please, double check your php.ini configuration and ensure you include mysql extension
Cheers!
Awesome. Thanks for the reply. How do I install that on a Windows machine? (I typically work on Ubuntu, but this is my gaming/hobby computer I've been doing this on).
[Edit] PS just checked. My php.ini has this line of code uncommented
extension=php_pdo_mysql.dll
and I have a file called php_pdo_mysql.dll in my ext folder in php\ext
Am I maybe missing some kind of screwy windows configuration setting or something?
Ah, it difficult to say for windows - it depends on how did you install PHP on it. I doesn't work on Windows, so I think you just need to google "How to install pdo_mysql PHP %your_version_here% extension for Windows".
Cheers!
Figured it out. Part of the confusion comes from the fact that php_msql.dll has been deprecated in php7 and replaced by php_mysqli.dll
My specific problem was I forgot to restart the php webserver after altering the php.ini file
Oh, interesting to know! I don't know this. And yes, forgetting restart server is the most common mistake - good investigation :)
Thanks. I'll take a look and let you know what I find...
I got same issue, and resolve it using this step:
1.) Installing pdo_mysql: apt-get install php7.0-pdo-mysql
2.) Write to symfony homepage phpinfo(); Then in browser see something like: "Loaded Configuration File: /etc/php7.0/cli/php.ini".
3.) sudo vi /etc/php7.0/cli/php.ini and uncommented "extension=php_pdo_mysql.dll"
4.) Restart PHP
5.) Restart Symfony process
Sorry for my english, just I recently started learning
Hey Aidas,
Thanks for sharing this with others!
Cheers!
"Houston: no signs of life"
Start the conversation!