Symfony2 get object from inverted entity with ManyToMany relation Symfony2 get object from inverted entity with ManyToMany relation symfony symfony

Symfony2 get object from inverted entity with ManyToMany relation


As follows from doctrine documentation:

mappedBy: This option specifies the property name on the targetEntity that is the owning side of this relation. Its a required attribute for the inverse side of a relationship.

inversedBy: The inversedBy attribute designates the field in the entity that is the inverse side of the relationship.

So, try to change inversedBy="Article" to inversedBy="articles" in Article class and mappedBy="Category" to mappedBy="categories" in Category class.

Also see this Many-To-Many bidirectional example.

Hope this helps.


All right then, I followed your advice and changed Article with articles, and Category by categories. I also changed my mapping for the master side, Articles, like that :

/** * @ORM\ManyToMany( *    targetEntity="Alpha\BlogBundle\Entity\Category", *    cascade={"persist"}, *    inversedBy="articles" * ) * @ORM\JoinTable(name="article_category", *      joinColumns={@ORM\JoinColumn(name="article_id", referencedColumnName="id")}, *      inverseJoinColumns={ *          @ORM\JoinColumn(name="category_id", referencedColumnName="id") *      } * ) */

It works like a charm. I don't exactly understand but I'm gonna study that :)