Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 75600c5

Browse filesBrowse files
hrbonzweaverryan
authored andcommitted
add yaml mappings for category <-> product onToMany, bidirectional relation
1 parent 6d56d93 commit 75600c5
Copy full SHA for 75600c5

File tree

Expand file treeCollapse file tree

1 file changed

+59
-24
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+59
-24
lines changed

‎book/doctrine.rst

Copy file name to clipboardExpand all lines: book/doctrine.rst
+59-24Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -770,25 +770,42 @@ Relationship Mapping Metadata
770770
To relate the ``Category`` and ``Product`` entities, start by creating a
771771
``products`` property on the ``Category`` class::
772772

773-
// src/Acme/StoreBundle/Entity/Category.php
774-
// ...
775-
use Doctrine\Common\Collections\ArrayCollection;
776-
777-
class Category
778-
{
773+
.. configuration-block::
774+
775+
.. code-block:: php-annotations
776+
777+
// src/Acme/StoreBundle/Entity/Category.php
779778
// ...
779+
use Doctrine\Common\Collections\ArrayCollection;
780780
781-
/**
782-
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
783-
*/
784-
protected $products;
785-
786-
public function __construct()
781+
class Category
787782
{
788-
$this->products = new ArrayCollection();
789-
}
783+
// ...
784+
785+
/**
786+
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
787+
*/
788+
protected $products;
789+
790+
public function __construct()
791+
{
792+
$this->products = new ArrayCollection();
793+
}
790794
}
791795

796+
.. code-block:: yaml
797+
798+
# src/Acme/StoreBundle/Resources/config/doctrine/Category.orm.yml
799+
Acme\StoreBundle\Entity\Category:
800+
type: entity
801+
# ...
802+
oneToMany:
803+
products:
804+
targetEntity: Product
805+
mappedBy: category
806+
# don't forget to init the collection in entity __construct() method
807+
808+
792809
First, since a ``Category`` object will relate to many ``Product`` objects,
793810
a ``products`` array property is added to hold those ``Product`` objects.
794811
Again, this isn't done because Doctrine needs it, but instead because it
@@ -813,19 +830,37 @@ makes sense in the application for each ``Category`` to hold an array of
813830
Next, since each ``Product`` class can relate to exactly one ``Category``
814831
object, you'll want to add a ``$category`` property to the ``Product`` class::
815832

816-
// src/Acme/StoreBundle/Entity/Product.php
817-
// ...
833+
.. configuration-block::
818834

819-
class Product
820-
{
835+
.. code-block:: php-annotations
836+
837+
// src/Acme/StoreBundle/Entity/Product.php
821838
// ...
822839
823-
/**
824-
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
825-
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
826-
*/
827-
protected $category;
828-
}
840+
class Product
841+
{
842+
// ...
843+
844+
/**
845+
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
846+
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
847+
*/
848+
protected $category;
849+
}
850+
851+
.. code-block:: yaml
852+
853+
# src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.yml
854+
Acme\StoreBundle\Entity\Product:
855+
type: entity
856+
# ...
857+
manyToOne:
858+
category:
859+
targetEntity: Category
860+
inversedBy: products
861+
joinColumn:
862+
name: category_id
863+
referencedColumnName: id
829864
830865
Finally, now that you've added a new property to both the ``Category`` and
831866
``Product`` classes, tell Doctrine to generate the missing getter and setter

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.