Referencing doctrine reference – one to many unidirectional
class User
{
// ...
/**
* @ManyToMany(targetEntity="Phonenumber")
* @JoinTable(name="users_phonenumbers",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)}
* )
*/
private $phonenumbers;
// ...
}
The part I don’t understand is unique=true. What does it do? The way I read it is …
- User has a Many to Many relationship with Phonenumber
- it uses the join table
users_phonenumbers users_phonenumbers.user_id = users.idusers_phonenumbers.phonenumber_id = Phonenumber.id- and I guess the
uniquedoes something to constraints a many to many to a many to one relationship somehow. But how do you explain it? Also in a SQL sense (what is the output like)?
The mapping translates into the following SQL tables (assuming both have a surrogate ID, called id):
What this means in terms of your code:
This would throw a unique constraint exception, you cannot add the same phonenumber to different users, because phonenumbers are unique (on the database level).