我有一个模式,其中实体“用户”和“角色”之间的多对多关系映射如下。
角色实体
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles", cascade={"persist", "remove"})
*/
protected $users;
和用户实体
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist", "remove"})
* @ORM\JoinTable(name="users_roles")
*
* @var ArrayCollection $userRoles
*/
protected $userRoles;
当我尝试使用以下代码删除角色对象时,
$role = $em->getRepository('ACMEDefaultBundle:Role')->find($id);
$em->remove($role);
$em->flush();
我遇到了 Doctrine 异常
ErrorException: Notice: Undefined index: roles in /media/sf_sandbox/aalcodev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 964 (uncaught exception) at /media/sf_sandbox/aalcodev/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 964
请帮忙。谢谢。
最佳答案
您应该将 mappedBy="roles"
更改为 mappedBy="userRoles"
。 “Mapped by”应该指向对方的属性名...
关于php - 删除与 EntityManager 的多对多关系的对象部分返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955449/