我有一个实现 UserInterface 以与 RBAC 系统一起使用的用户实体。我还没有实现整个系统。但是,当我尝试使用以下代码删除用户时,该操作会删除其他表中的所有用户和其他关联对象,然后引发错误。我能够毫无问题地从其他实体中删除对象。
用户实体
class User implements UserInterface
{
**
* @var integer $id
*
* @ORM\Column(name="id", type="smallint")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
protected $id;
**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=20, unique=TRUE)
*
protected $username;
**
* @var string $password
*
* @ORM\Column(name="password", type="string", length=255)
*
protected $password;
**
* @var string $salt
*
* @ORM\Column(name="salt", type="string", length=255)
*
protected $salt;
**
* @var string $fullName
*
* @ORM\Column(name="full_name", type="string", length=60, unique=TRUE)
*
protected $fullName;
**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"persist", "remove"})
* @ORM\JoinTable(name="users_roles")
*
* @var ArrayCollection $userRoles
*
protected $userRoles;
public function __construct()
{
$this->userRoles = new ArrayCollection();
}
}
删除操作
public function deleteUserAction($id) {
$user = $em->getRepository('ACMECompanyBundle:User')->find($id);
$currentUser = $this->get('security.context')->getToken()->getUser();
if ($id == $currentUser->getId()) {
return new Response("You cannot delete the current user");
}
if (!$user) {
throw $this->createNotFoundException('No user found for id '.$id);
}
try {
$em->remove($user);
$em->flush();
$msg = "User deleted!";
$code = "OK";
} catch (DBALException $e) {
return new Response($e);
$msg = "User cannot be deleted!";
$code = "ERR";
}
$response = new Response(json_encode(array('code' => $code, 'msg' => $msg)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
删除所有用户后返回的错误是
InvalidArgumentException:您无法从不包含标识符的 EntityUserProvider 刷新用户。用户对象必须使用由 Doctrine 映射的自己的标识符进行序列化。