Symfony2:FOSUserBundle删除操作错误

时间:2015-02-10 04:12:55

标签: php symfony fosuserbundle

Symfony2相当新鲜;我现在使用FOSUserBundle作为我的userprovider。由于它没有为删除操作提供控制器,我自己写了:

    /**
         * 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");
    }

奇怪的是,我得到了相同的结果:实体从数据库中删除。

由于某种原因,我的路线似乎没有传递参数。

  

DEBUG - SELECT 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 AS confirmation_token12,   t0.password_requested_at AS password_requested_at13,t0.roles AS   roles14,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

2 个答案:

答案 0 :(得分:2)

尝试将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);
    }

我希望它有所帮助。

答案 1 :(得分:2)

获得用户经理时

$um = $this->container->get('fos_user.user_manager')

您可以在获取用户操作后使用删除操作:

$um->deleteUser($user);

但您需要先检查用户是否为空。