【问题标题】:Cannot Delete User - QueryException: DELETE WHERE u.id = :id无法删除用户 - QueryException: DELETE WHERE u.id = :id
【发布时间】:2017-05-19 22:29:42
【问题描述】:

我在 Symfony 中工作 - 当我执行删除用户的操作时,我收到以下错误消息:

[Semantical Error] line 0, col 7 near 'WHERE u.id =': Error: Class 'WHERE' is not defined.
500 Internal Server Error - QueryException
1 linked Exception:

QueryException » 

1/2 QueryException: DELETE WHERE u.id = :id
2/2 QueryException: [Semantical Error] line 0, col 7 near 'WHERE u.id =': Error: Class 'WHERE' is not defined.

管理员控制器

 /**
 * @Route("/admin/user/delete/{id}", name="admin_user_delete")
 * @Template
 */
public function deleteAction($id)
{
    $dispatcher = $this->container->get('event_dispatcher');

    $this->get('users')->delete($id);

    // create the user event and dispatch it
    $event = new UserEvent($id);
    $dispatcher->dispatch(UserEvents::USER_DELETED, $event);

    $this->get('session')->getFlashBag()->add('notice-success', 'User deleted successfully.');

    return $this->redirect($this->generateUrl('admin_user_list'));
}

User.php 服务

public function delete($id)
{
    $this->repository->delete($id);
}

用户存储库

public function delete($id)
{
    $qb = $this->getEntityManager()->createQueryBuilder('u');
    $qb->delete()
        ->andWhere($qb->expr()->eq('u.id', ':id'))
        ->setParameter(':id', $id)
        ->getQuery()
        ->getResult();
}

【问题讨论】:

  • 您没有在setParameter() 调用中使用: 字符。 docs.doctrine-project.org/projects/doctrine-orm/en/latest/…
  • 已修复,但仍然存在相同的错误
  • 您是要在控制器或实体文件中删除吗?
  • : 实际上是无关紧要的。 DQL 适用于对象而不是 id。看看这里接受的答案:stackoverflow.com/questions/15555042/… 您需要创建对用户对象的引用。我认为 $em->remove($user) 后跟一个刷新将是一种更清洁的方法。
  • 感谢@Cerad - 这看起来是正确的方向。这对我来说是一个相当新的环境——你有没有机会指导我把它写出来?我一直在玩,但不及格。

标签: php symfony


【解决方案1】:

使用查询生成器删除用户的方法

public function delete($id)
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    return $qb->delete('Bundle:User', 'u')
        ->where('u.id = :id'))
        ->setParameter('id', $id)
        ->getQuery()
        ->getResult();
}

如何使用内置 ORM 删除用户

public function delete($id) {
    // get access to the entity manager
    $em = $this->getEntityManager();
    // get the user from the repository by id
    // previous version, slightly slower as it executes a query
    // $user = $em->getRepository('Bundle:User')->find($id);

    // Cerads method I just found out about myself, even better version!
    // this doesn't execute a query but gets a reference to the user object 
    // and sets the id
    $user = $em->getReference('Bundle:User', $id);

    // use built in methods to delete the user
    $em->remove($user);
    // update the database with the change
    $em->flush();
}

【讨论】:

  • 在第二种方法中,将 find 替换为 getReference($id)。无需加载整个用户对象来删除它。你真的尝试过第一种方法吗?不肯定你说得很对。很确定你会需要像 Bundle:User 这样的东西。
  • 你在这两个方面都是对的,我只是让用户不认为阅读用户可能不知道通过捆绑访问它。我实际上并不知道 getReference() 调用,但它非常有用。谢谢!
  • @JacobW 使用您的方法时,我收到Unknown Entity namespace alias 'Bundle'. 500 Internal Server Error - ORMException
  • @RKlina - 我认为你已经知道足以惹上麻烦了。稍作停顿,然后返回阅读 Doctrine 章节:symfony.com/doc/current/doctrine.html 第二轮事情应该开始变得更有意义了。
  • 确实如此。将重新访问并重新加入 - 非常感谢您的意见!
【解决方案2】:

正如 Jared Farrish 上面所说,setParameter 调用中不需要冒号。

应该是:

->setParameter('id', $id)

【讨论】:

  • 谢谢 - 阅读发布的 docs.doctrine... 修复后仍然存在相同的错误。
  • 正如我在评论中所说,不是答案。 ;)
猜你喜欢
  • 2020-10-18
  • 2013-10-11
  • 2017-08-30
  • 2018-10-16
  • 2019-04-21
  • 1970-01-01
  • 2015-12-05
  • 1970-01-01
  • 2012-08-08
相关资源
最近更新 更多