i have a Gift entity, this entity has a sender, and a receiver…both from the entity User
a user can be a sender of many gifts to other users. but also a receiver of many gifts from many users
the way i see it, the solution inside my Gift entity would be like this:
/**
* @ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* @ORM\JoinTable(name="gift_user",
* joinColumns={@ORM\JoinColumn(name="sender_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $senders;
/**
* @ORM\ManyToMany(targetEntity="Tracker\UserBundle\Entity\User")
* @ORM\JoinTable(name="gift_user",
* joinColumns={@ORM\JoinColumn(name="receiver_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="gift_id", referencedColumnName="id")}
* )
*/
protected $receivers;
but when i run php app/console doctrine:schema:update --dump-sql i get:
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'finaldb.gift_user' already exists.
how do i have to change my syntax, configuration, so i end up with a table like this?
gift_id | sender_id | receiver_id
What you are trying to achieve is not possible without two join tables.
Each
@ManyToManyassociation requires a different join table. That because the join table does not know anything else than two linked entities (without knowing the direction of the association). Also, join tables generated by Doctrine ORM don’t have any auto-incremental identifier, since the two references already represent the table’s primary key.