Image for post: Symfony2, working with Doctrine entities

Symfony2, working with Doctrine entities

Let's see some commands and snippet to interact with Doctrine2. If you use Netbeans IDE, you'll see how easy is to call the entityManager and other objects of this ORM. But remember to put all queries on your model and keep the controller functions as clean as you can.

Enum annotation

Doctrine is integrated with Symfony but I've had some problems using MySQL. Set your connection parameters is easy but I suggest to set enum annotations on your configurations. Check out this question and the second answer on StackOverflow.

Generate entities

Import XML doctrine files from an existing database to your bundle:

php app/console doctrine:mapping:import --force AcmeApplicationBundle xml

Generate PHP entities files on your src dir of your bundle. I prefer to use only a bundle to store entities. It's faster when you want to update a single or all of them.

php app/console doctrine:mapping:convert annotation ./src

Use this command to generate a single entity:

php app/console doctrine:generate:entity

Generate getters and setters:

php app/console doctrine:generate:entities AcmeApplicationBundle

MySQL Year field type

Doctrine has problems, one of these is working with a MySQL date year field type. Symfony (and even Zend) apps don't work when you select these field types. Solution: check out this question on StackOverflow. The last answer worked for me too, update the notation with this one on your field name:

/** * @var \Year * * @ORM\Column(name="year_born", columnDefinition="YEAR", nullable=false) */private $yearBorn;