错误:预期原则\ORM\查询\词法分析器::T_WITH,得到“ON”

2022-08-30 19:46:03

我编写了以下代码用于从数据库获取数据:

function getnotificationAction()
{
    $session = $this->getRequest()->getSession();
    $userId = $session->get('userid');

    $entitymanager = $this->getDoctrine()->getEntityManager();
    $notification = $entitymanager->getRepository('IGCNotificationBundle:Notifications');
    $userNotification = $entitymanager->getRepository('IGCNotificationBundle:Usernotifications');
    $query = $entitymanager
                 ->createQuery("SELECT n.notificationid, n.title,n.notificationmessage, u.creationdate, u.notificationid, u.messagestatus From IGCNotificationBundle:Notifications AS n JOIN IGCNotificationBundle:Usernotifications AS u ON u.notificationid = n.notificationid WHERE u.userId = :userId ORDER BY n.creationdate DESC")->setParameter('userId', userId);

    $notifications = $query->getResult();

    return $this->render('IGCNotificationBundle:Default:notification.html.twig', array('notifications' => $notifications));
} }

但它正在给予:

[语法错误] 第 0 行,列 203:错误:预期原则\ORM\Query\词法分析器::T_WITH,获取“ON” 500 内部服务器错误 - 查询异常 1 链接异常:查询异常 »


答案 1
[Syntax Error] line 0, col 203: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'ON' 500 Internal Server Error - QueryException 1 linked Exception: QueryException »

我认为您应该将关键字“ON”替换为“WITH”。

从文档中提取 :

现在,在 DQL 中,可以使用语法进行任意实体之间的联接。FROM Foo f JOIN Bar b WITH f.id = b.id


答案 2

请使用波纹管代码,它的工作为您服务

function getnotificationAction() {
    $session = $this->getRequest()->getSession();
    $userId = $session->get('userid');
    $entitymanager = $this->getDoctrine()->getEntityManager();
    $notification = $entitymanager->getRepository('IGCNotificationBundle:Notifications');
    $userNotification = $entitymanager->getRepository('IGCNotificationBundle:Usernotifications');
    $query = $entitymanager->createQuery("SELECT n.notificationid, n.title,n.notificationmessage, u.creationdate, u.notificationid, u.messagestatus From IGCNotificationBundle:Notifications AS n JOIN IGCNotificationBundle:Usernotifications AS u WITH u.notificationid = n.notificationid WHERE u.userId = :userId ORDER BY n.creationdate DESC")->setParameter('userId', userId);
    $notifications = $query->getResult();
    return $this->render('IGCNotificationBundle:Default:notification.html.twig', array('notifications' => $notifications));
}

推荐