【发布时间】: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 - 这看起来是正确的方向。这对我来说是一个相当新的环境——你有没有机会指导我把它写出来?我一直在玩,但不及格。