Editing
Databases and Symfony
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Entity classes == Entity classes define the basic mapping to a table in the database. === Configuring entity classes === Classes that represent table data go in the `Entity` directory inside of `AppBundle`.<ref>[http://symfony.com/doc/current/book/doctrine.html Doctrine Documentation], Symfony Documentation, "The Book"</ref> Columns are represented by properties of the class. Typically these are `protected` and accessed with `public` "getter" and "setter" functions. <syntaxhighlight lang="php" highlight="6-8,10,15"> // src/AppBundle/Entity/Product.php namespace AppBundle\Entity; class Product { protected $id; protected $name; protected $description; public function getId() { return ($this->id); } public function setID($id) { $this->id = $id; } /* etc... */ } </syntaxhighlight> The getter and setter routines can be generated with <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entities AppBundle </syntaxhighlight> or <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entities AppBundle/Entity/Product </syntaxhighlight> To create an entity class solely from the command line: <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entity \ --entity="AppBundle:Category" \ --fields="name:string(255)" </syntaxhighlight> === Mapping information === Mapping metadata can be specified with YAML, XML, or directly in the entity class via annotations:<ref>[http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#property-mapping Doctrine Property Mapping]</ref>,<ref>[http://symfony.com/doc/current/book/doctrine.html#book-doctrine-field-types Doctrine Field Types Reference]</ref><ref>[http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#annref-column Annotations Reference], Doctrine documentation</ref> <syntaxhighlight lang="php" highlight="4,7-8,13-15,20"> // src/AppBundle/Entity/Product.php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="product") */ class Product { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") */ protected $name; /* etc... */ } </syntaxhighlight> === Generating database tables and/or schema === Doctrine can create and update tables in the database using the entity classes as templates with: <syntaxhighlight lang="bash"> $ php app/console doctrine:schema:update --force </syntaxhighlight> Changes to the properties and mapping of the entity class will cause updates that will attempt to preserve the existing data. The preferred way to generate changes that can be applied to a production environment is with [http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html migrations] which generate SQL statements that are stored in migration classes.
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information