@@ -770,25 +770,42 @@ Relationship Mapping Metadata
770
770
To relate the ``Category `` and ``Product `` entities, start by creating a
771
771
``products `` property on the ``Category `` class::
772
772
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
779
778
// ...
779
+ use Doctrine\Common\Collections\ArrayCollection;
780
780
781
- /**
782
- * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
783
- */
784
- protected $products;
785
-
786
- public function __construct()
781
+ class Category
787
782
{
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
+ }
790
794
}
791
795
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
+
792
809
First, since a ``Category `` object will relate to many ``Product `` objects,
793
810
a ``products `` array property is added to hold those ``Product `` objects.
794
811
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
813
830
Next, since each ``Product `` class can relate to exactly one ``Category ``
814
831
object, you'll want to add a ``$category `` property to the ``Product `` class::
815
832
816
- // src/Acme/StoreBundle/Entity/Product.php
817
- // ...
833
+ .. configuration-block ::
818
834
819
- class Product
820
- {
835
+ .. code-block :: php-annotations
836
+
837
+ // src/Acme/StoreBundle/Entity/Product.php
821
838
// ...
822
839
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
829
864
830
865
Finally, now that you've added a new property to both the ``Category `` and
831
866
``Product `` classes, tell Doctrine to generate the missing getter and setter
0 commit comments