【问题标题】:Twice Join in Symfony2 with Doctrine (Syntax Error)使用 Doctrine 两次加入 Symfony2(语法错误)
【发布时间】:2015-02-17 14:52:18
【问题描述】:

我有桌子:

Car(Auto) ->(1:N) Rent (N:1) <- Department(Abteilung)

我想通过内部连接将它们连接在一起。在 Rent 中是部门和汽车的 ID。

如果我两次加入,我会收到错误消息。一次加入它就可以工作。这是为什么?我该如何解决这个问题?

$result = $this->getDoctrine()->getRepository('ChrisKfzBuchungBundle:Rent')
            ->createQueryBuilder('r')
            ->innerJoin('ChrisKfzBuchungBundle:Rent','ChrisKfzBuchungBundle:Auto')
            ->innerJoin('ChrisKfzBuchungBundle:Rent','ChrisKfzBuchungBundle:Abteilung')
            ->where('r.mieteStart >= :date_from')
            ->andWhere('r.mieteEnde <= :date_to')
            ->setParameter('date_from', $date_from)
            ->setParameter('date_to', $date_to)
            ->distinct()
            ->getQuery()->getArrayResult();

[语法错误] line 0, col 129: Error: Expected Literal, got 'JOIN'

谢谢!

【问题讨论】:

  • 尝试像这样进行连接:-&gt;innerJoin('r.auto', 'auto)-&gt;innerJoin('r.abteilung', 'abteilung)
  • 那么没有语法错误,但我没有得到表'abteilung'的结果
  • 这是因为你只在-&gt;createQueryBuilder('r')选择了'r'
  • 我在没有连接的情况下测试它,连接是无用的,他们不会提供更多的数据然后没有。那是什么?
  • 当然,在你的情况下,连接没有任何作用,因为你没有在它们之间检索数据。在您的查询中,您只有selection Rents where date is between。仅此而已。

标签: php symfony join doctrine-orm


【解决方案1】:

这行得通,感谢 manix 的帮助。我必须更正联接并使用 addSelect(我不知道的命令)。

 $result = $this->getDoctrine()->getRepository('ChrisKfzBuchungBundle:Rent')
            ->createQueryBuilder('r')
            ->addSelect('abteilung')
            ->addSelect('auto')
            ->join('r.auto','auto')
            ->join('r.abteilung','abteilung')
            ->where('r.mieteStart >= :date_from')
            ->andWhere('r.mieteEnde <= :date_to')
            ->setParameter('date_from', $date_from)
            ->setParameter('date_to', $date_to)
            ->distinct()
            ->getQuery()->getArrayResult();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    相关资源
    最近更新 更多