Это потому, что вы не указали mappedBy (владеющую сторону) и inversedBy.
Проверьте, как много-много-много отношений между пользователями и ролями:
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\ManyToMany(targetEntity="Role", mappedBy="users")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
protected $roles;
/**
* Constructor
*/
public function __construct()
{
$this->roles = new ArrayCollection();
}
/**
* Has role
*
* @param Role $role
* @return bool
*/
public function hasRole(Role $role)
{
return $this->roles->contains($role);
}
/**
* Add role
*
* @param Role $role
*/
public function addRole(Role $role)
{
if (!$this->hasRole($role)) {
$this->roles->add($role);
$role->addUser($this);
}
}
/**
* Remove roles
*
* @param Role $role
*/
public function removeRole(Role $role)
{
if ($this->hasRole($role)) {
$this->roles->removeElement($role);
$role->removeUser($this);
}
}
}
.
/**
* @ORM\Entity
* @ORM\Table(name="role")
*/
class Role implements RoleInterface
{
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="roles")
* @ORM\JoinTable(name="user_role",
* joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* Has user
*
* @param User $user
* @return bool
*/
public function hasUser(User $user)
{
return $this->users->contains($user);
}
/**
* Add user
*
* @param User $user
*/
public function addUser(User $user)
{
if (!$this->hasUser($user)) {
$this->users->add($user);
$user->addRole($this);
}
}
/**
* Remove user
*
* @param User $user
*/
public function removeUser(User $user)
{
if ($this->hasUser($user)) {
$this->users->removeElement($user);
$user->addRole($this);
}
}
/**
* Get users
*
* @return ArrayCollection
*/
public function getUsers()
{
return $this->users;
}