Symfony Database Cookbook: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Category:Symfony Category:PHP Category:Web Development == Creating classes with Doctrine == A command line shortcut for the process of creating an entity class d...") |
|||
| Line 13: | Line 13: | ||
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> | 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"> | <syntaxhighlight lang="php" highlight="3,9-12,16"> | ||
// src/AppBundle/Entity/Category.php | // src/AppBundle/Entity/Category.php | ||
| Line 34: | Line 34: | ||
} | } | ||
</syntaxhighlight> | </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. | |||
== Notes == | == Notes == | ||
<references/> | <references/> | ||
Revision as of 00:33, 25 January 2015
Creating classes with Doctrine
A command line shortcut for the process of creating an entity class described in Generating Database Tables and/or Schema:
$ php app/console doctrine:generate:entity --entity="AppBundle:Category" --fields="name:string(100)"
Specifying foreign keys between tables
First create a property within the category/group/type class that will hold the records linked to it.[1]
// 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();
}
}
Then add a property in the other class to reference the category/group/type class:
// src/AppBundle/Entity/Product.php
// ...
class Product
{
// ...
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
protected $category;
}
Have Doctrine generate getter and setter methods:
$ php app/console doctrine:generate:entities Acme
Examples
- Tutorials database:
TutorialandTutorialGroupclasses.
Notes
- ↑ Relationships/Accosciations, Doctrine documentation, Symfony documentation, "The Book"