drydaenth257216154 2015-02-10 04:12
浏览 47
已采纳

Symfony2:FOSUserBundle删除操作错误

Fairly new to Symfony2; I am now using FOSUserBundle as my userprovider. Since it doesn't provide a controller for delete actions, I wrote my own:

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

And added a link to the path from my template:

    <a href="{{ path('star9988_user_user_delete', {'id': user.id}) }}">

I had initially tried passing the ObjectManager in, querying for the User object by ID, and then deleting that with the deleteUser method, but for some reason, it wouldn't let me fetch the ObjectManager using the usual constructor injection method.

As a result, I have resorted to calling the EntityManager, and calling its remove() method on my $user object.

The result is very strange: when I check my database with SequelPro, I notice that the specified user object is indeed deleted. However, the browser returns this error:

EntityManager#remove() expects parameter 1 to be an entity object, NULL given.

Any help would be much appreciated.

EDIT:

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

Strangely, I'm getting the same result: the entity is deleted from the database.

It seems that my route is not passing the parameter for some reason.

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 = ? LIMIT 1

  • 写回答

2条回答 默认 最新

  • duanji1026 2015-02-10 08:58
    关注

    Try setting the id as a required parameter, like in this example:

    /**
     * @Route("
     *    path          = "/delete/article/{id}", 
     *    name          = "blog_admin_delete_article"
     *    requirements  = { "id" = "\d+" },
     *    methods       = { "GET" }
     * )
     */
    public function deleteArticleAction($id) { /* ... */ }
    

    Also you can check in the code that the user was loaded:

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

    I hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密