I have two Entities: Role
and Right
. There's a many to many relationship between them. The joining table role_rights
has no entity defined. It has two fields: role_id
and right_id
. So I have done following:
Role Entity
class Role
{
/**
* Many Roles have Many Rights.
* @ORM\ManyToMany(targetEntity="Right")
* @ORM\JoinTable(name="role_has_right",
* joinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="right_id", referencedColumnName="id")}
* )
*/
private $rights;
public function __construct()
{
$this->rights = new ArrayCollection();
$this->creationDate = new \DateTime("now");
$this->updateDate = new \DateTime("now");
$this->deleteDate = null;
}
/**
* @param Right $right
*/
public function addRight($right)
{
$this->rights[] = $right;
}
}
Role Data Access
class RoleDAO extends EntityRepository {
public function assignRoles($role_id,$right_id) {
$role_info = $this->find($role_id);
$right_info = $this->find($right_id);
$role_info->addRight($right_info);
$em = $this->getEntityManager();
$em->persist($role_info);
$em->flush();
}
}
And it's giving error:
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "App\Model\Entities\Role#$rights", got "App\Model\Entities\Role" instead.
Goal is to add role and relative rights'IDs in mapped table.
Thanks