Symfony2:FOSUserBundle 删除操作错误
Posted
技术标签:
【中文标题】Symfony2:FOSUserBundle 删除操作错误【英文标题】:Symfony2: FOSUserBundle Delete Action Error 【发布时间】:2015-02-10 04:12:55 【问题描述】:对 Symfony2 相当陌生;我现在使用 FOSUserBundle 作为我的用户提供程序。由于它不提供删除操作的控制器,所以我自己编写了:
/**
* Delete user
*
* @param integer $id
*
* @Route("/delete/id")
*
* @return RedirectResponse
*/
public function deleteAction($id)
$em = $this->getDoctrine()->getManager();
$um = $this->get('fos_user.user_manager');
$user = $um->findUserBy(array(
'id' => $id
)
);
$em->remove($user);
$em->flush();
return new RedirectResponse("star9988_user_user_index");
并从我的模板中添加了指向路径的链接:
<a href=" path('star9988_user_user_delete', 'id': user.id) ">
我最初尝试传入 ObjectManager,通过 ID 查询 User 对象,然后使用 deleteUser 方法将其删除,但由于某种原因,它不允许我使用通常的构造函数注入方法获取 ObjectManager。
因此,我求助于调用 EntityManager,并在我的 $user 对象上调用它的 remove() 方法。
结果很奇怪:当我用 SequelPro 检查我的数据库时,我注意到指定的用户对象确实被删除了。但是,浏览器返回此错误:
EntityManager#remove() 期望参数 1 是一个实体对象, 给定 NULL。
任何帮助将不胜感激。
编辑:
I have changed my code to the following:
/**
* Delete user
*
* @param integer $id
*
* @Route("/delete/id")
*
* @return RedirectResponse
*/
public function deleteAction($id)
$em = $this->getDoctrine()->getManager();
/** @var \Star9988\ModelBundle\Entity\User $user */
$user = $this->getDoctrine()->getRepository('ModelBundle:User')->find($id);
$em->remove($user);
$em->flush();
return new RedirectResponse("star9988_user_user_index");
奇怪的是,我得到了相同的结果:实体已从数据库中删除。
看来我的路由由于某种原因没有传递参数。
调试 - 选择 t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token 作为confirmation_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS 角色14,t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.id AS id17, t0.FirstName AS FirstName18, t0.LastName AS LastName19, t0.creditLimit AS creditLimit20, t0.creditAvailable AS creditAvailable21, t0.accountBalance AS accountBalance22, t0.rebate AS rebate23, t0.parent AS parent24 FROM StarUsers t0 WHERE t0.id = ?限制 1 个
【问题讨论】:
FOSUserBundle 带有一个用户管理器github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Doctrine/…,它允许您执行所有这些操作:github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/… 【参考方案1】:尝试将 id 设置为必需参数,如this example:
/**
* @Route("
* path = "/delete/article/id",
* name = "blog_admin_delete_article"
* requirements = "id" = "\d+" ,
* methods = "GET"
* )
*/
public function deleteArticleAction($id) /* ... */
您还可以签入加载用户的代码:
/** @var \Star9988\ModelBundle\Entity\User $user */
$user = $this->getDoctrine()->getRepository('ModelBundle:User')->find($id);
if (!$user instanceof User)
throw new NotFoundHttpException('User not found for id ' . $id);
希望对你有帮助。
【讨论】:
【参考方案2】:当你得到用户管理器时
$um = $this->container->get('fos_user.user_manager')
您可以在获得这样的用户操作后使用删除:
$um->deleteUser($user);
但你需要在之前检查用户是否为空。
【讨论】:
以上是关于Symfony2:FOSUserBundle 删除操作错误的主要内容,如果未能解决你的问题,请参考以下文章
Symfony2 - 使用 FOSUserBundle 进行测试
symfony2:使用 fosuserbundle 登录后使用引用
获取 FOSUserBundle 组的用户 (Symfony2)
FOSUserBundle 使用电子邮件登录 (Symfony2)