Editing
Symfony Database Cookbook
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!
[[Category:Symfony]] [[Category:PHP]] [[Category:Web Development]] == Creating classes with Doctrine == A command line shortcut for the process of creating an entity class described in [[Databases_and_Symfony#Generating_database_tables_and.2For_schema|Generating Database Tables and/or Schema]]: <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entity --entity="AppBundle:Category" --fields="name:string(100)" </syntaxhighlight> == Specifying foreign keys between tables == First create a property within the category/group/type class that will hold the records linked to it.<ref>[http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associationsEntity Relationships/Accosciations], Doctrine documentation, Symfony documentation, "The Book"</ref> <syntaxhighlight lang="php" highlight="4,10-12,17"> // src/AppBundle/Entity/Category.php // ... use Doctrine\Common\Collections\ArrayCollection; class Category { // ... /** * @ORM\OneToMany(targetEntity="Product", mappedBy="category") */ protected $products; public function __construct() { $this->products = new ArrayCollection(); } } </syntaxhighlight> Then add a property in the other class to reference the category/group/type class: <syntaxhighlight lang="php" highlight="9-10,12"> // src/AppBundle/Entity/Product.php // ... class Product { // ... /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category; } </syntaxhighlight> Have Doctrine generate getter and setter methods: <syntaxhighlight lang="bash"> $ php app/console doctrine:generate:entities Acme </syntaxhighlight> === Examples === * [http://tutorials.dbarchowsky.com Tutorials database]: `Tutorial` and `TutorialGroup` classes. == Selectively retrieving object properties == By default Doctrine retrieves all the properties for an object.<ref>[http://www.uvd.co.uk/blog/some-doctrine-2-best-practices/ Some Doctrine 2 Best Practices], UVD<br />#4 By default Doctrine will fetch all of the properties for a given entity</ref> <syntaxhighlight lang="php"> $qb = $em->createQueryBuilder(); // You can select multiple properties in the partial statement $qb ->select('partial Article.{id,title,summary}') ->from('Entity\Article') ; </syntaxhighlight> == Controlling the order of associated entities == With a one-to-many relationship, display the groups, with the associated entities under each group. In the group entity add an OrderBy annotation to the associated property: <syntaxhighlight lang="php" highlight="4"> /** * @var Array List of tutorials in this group. * @ORM\OneToMany(targetEntity="Tutorial", mappedBy="group") * @ORM\OrderBy({"slot" = "ASC", "date" = "DESC"}) */ public $tutorials; </syntaxhighlight> == See also == * [[DQL User-Defined Functions]] (wiki) * [http://labs.octivi.com/mastering-symfony2-performance-doctrine/ Mastering Symfony2 Performance] (Octivi.com) == Notes == <references/>
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