3
votes

into Doctrine 2 Documentation explain Owning Side and Inverse Side into ManyToMany Relationship and said that :

For ManyToMany bidirectional relationships either side may be the owning side (the side that defines the @JoinTable and/or does not make use of the mappedBy attribute, thus using a default join table).

Is That mean I can write my annotation without using inversedBy and mappedBy To refer for Owning Side of ManyToMany Relationship and Inverse Side of ManyToMany Relationship ?

Ex:

can i write that :

Affiliate Entity

   /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Category")
     *      
     */        
    private $categories;

and

Category Entity

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Affiliate")
 * @ORM\JoinTable(name="category_affiliate")
 *      
 */
private $afflitiates;

instead of write that:

Affiliate Entity

   /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Category", mappedBy="afflitiates")
     *      
     */        
    private $categories;

and

Category Entity

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Affiliate",inversedBy="categories")
 * @ORM\JoinTable(name="category_affiliate")
 *      
 */
private $afflitiates;
2
why don't you just try it out? ;) little hint: The side which has @ORM\JoinTable is automatically the owning side ... so it's npossible for Doctrine to determine the owning side without needing an inversedBy attribute, right? - Nicolai Fröhlich
i had tried and i notice that: the two ways is correct - ahmed hamdy

2 Answers

3
votes

Incase of Many to Many relationship, you can chose any one side as your owning side and the other automatically turns to be the inversing side. But try to check which entity that you will frequent trigger to get the objects and manage your owning side

0
votes

My summary as an answer to question:

(1) Firstly, determine the owning side. Example, Category entity is owning/map side, hence InverseBy this $category + JoinTable

(2) Hence Affiliate entity is inverse side, hence MappedBy this $affiliate.

Another way:

(1) InversedBy (2) using (1a) + JointTable

(2) MappedBy (1) using (2a)

it is the wordings confused us.

D.