1-33
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Symfony2 – Franz Jordán 2011<br />
Be careful that your class name and properties aren't mapped to a protected SQL keyword (such<br />
as group or user). For example, if your entity class name is Group, then, by default, your table<br />
name will be group, which will cause an SQL error in some engines. See Doctrine's Reserved SQL<br />
keywords documentation on how to properly escape these names.<br />
When using another library or program (ie. Doxygen) that uses annotations, you should place<br />
the @IgnoreAnnotation annotation on the class to indicate which annotations Symfony should<br />
ignore.<br />
For example, to prevent the @fn annotation from throwing an exception, add the following:<br />
/**<br />
* @IgnoreAnnotation("fn")<br />
*/<br />
class Product<br />
Generating Getters and Setters<br />
Even though Doctrine now knows how to persist a Product object to the database, the class<br />
itself isn't really useful yet. Since Product is just a regular PHP class, you need to create getter<br />
and setter methods (e.g. getName(), setName()) in order to access its properties (since the<br />
properties areprotected). Fortunately, Doctrine can do this for you by running:<br />
php app/console doctrine:generate:entities Acme/StoreBundle/Entity/Product<br />
This command makes sure that all of the getters and setters are generated for<br />
the Product class. This is a safe command - you can run it over and over again: it only<br />
generates getters and setters that don't exist (i.e. it doesn't replace your existing methods).<br />
The doctrine:generate:entities command saves a backup of the<br />
originalProduct.php named Product.php~. In some cases, the presence of this file can cause a<br />
"Cannot redeclare class" error. It can be safely removed.<br />
You can also generate all known entities (i.e. any PHP class with Doctrine mapping information)<br />
of a bundle or an entire namespace:<br />
php app/console doctrine:generate:entities AcmeStoreBundle<br />
php app/console doctrine:generate:entities Acme<br />
Doctrine doesn't care whether your properties are protected or private, or whether or not you<br />
have a getter or setter function for a property. The getters and setters are generated here only<br />
because you'll need them to interact with your PHP object.<br />
Creating the Database Tables/Schema<br />
You now have a usable Product class with mapping information so that Doctrine knows exactly<br />
how to persist it. Of course, you don't yet have the corresponding product table in your<br />
database. Fortunately, Doctrine can automatically create all the database tables needed for every<br />
known entity in your application. To do this, run:<br />
php app/console doctrine:schema:update --force<br />
Actually, this command is incredibly powerful. It compares what your databaseshould look like<br />
(based on the mapping information of your entities) with how itactually looks, and generates the<br />
SQL statements needed to update the database to where it should be. In other words, if you add<br />
86