Join table required in bidirectional many to many? #5952

Open
opened 2026-01-22 15:23:05 +01:00 by admin · 0 comments
Owner

Originally created by @hvanoch on GitHub (Apr 17, 2018).

If I do not specify a join table on the many to many relation definition then doctrine schema valdiate command complains:

[FAIL] The entity-class AppBundle\Entity\Client mapping is invalid:

  • The field AppBundle\Entity\Client#users is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Application\Sonata\UserBundle\Entity\User#clients does not contain the required 'inversedBy="users"' attribute.

Code:

Annotations for the Client class

    /**
     * @var \Doctrine\Common\Collections\Collection|\Application\Sonata\UserBundle\Entity\User[]
     *
     * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="clients")
     */
    private $users;

XML for the User class
<many-to-many field="clients" target-entity="AppBundle\Entity\Client" inversed-by="users" />

Class looks likes:

    /** @var \Doctrine\Common\Collections\Collection|\AppBundle\Entity\Client[] */
    protected $clients;

If I change the xml definition like below, the validate command does not fail.

        <many-to-many field="clients" target-entity="AppBundle\Entity\Client" inversed-by="users">
            <join-table name="client_users">
                <join-columns>
                    <join-column name="client_id" />
                </join-columns>
                <inverse-join-columns>
                    <join-column name="user_id"/>
                </inverse-join-columns>
            </join-table>
        </many-to-many>

doctrine 2.6.1

Originally created by @hvanoch on GitHub (Apr 17, 2018). If I do not specify a join table on the many to many relation definition then doctrine schema valdiate command complains: > [FAIL] The entity-class AppBundle\Entity\Client mapping is invalid: > * The field AppBundle\Entity\Client#users is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity Application\Sonata\UserBundle\Entity\User#clients does not contain the required 'inversedBy="users"' attribute. > Code: Annotations for the Client class ``` /** * @var \Doctrine\Common\Collections\Collection|\Application\Sonata\UserBundle\Entity\User[] * * @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="clients") */ private $users; ``` XML for the User class ` <many-to-many field="clients" target-entity="AppBundle\Entity\Client" inversed-by="users" />` Class looks likes: ``` /** @var \Doctrine\Common\Collections\Collection|\AppBundle\Entity\Client[] */ protected $clients; ``` If I change the xml definition like below, the validate command does not fail. ``` <many-to-many field="clients" target-entity="AppBundle\Entity\Client" inversed-by="users"> <join-table name="client_users"> <join-columns> <join-column name="client_id" /> </join-columns> <inverse-join-columns> <join-column name="user_id"/> </inverse-join-columns> </join-table> </many-to-many> ``` doctrine 2.6.1
admin added the BugMissing Tests labels 2026-01-22 15:23:05 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5952