【问题标题】:doctrine query builder SELECT DISTINCT throws error学说查询生成器 SELECT DISTINCT 抛出错误
【发布时间】:2018-10-02 07:25:14
【问题描述】:

这应该是直截了当的。我确实在 SO 和其他地方找到了一些关于这个主题的帖子,但它只是抛出了错误:

[语义错误] 第 0 行,第 18 列“线程 FROM App\Entity\Message”附近:错误:无效的 PathExpression。必须是 StateFieldPathExpression。

这用于在消息模块上选择不同的线程。我尝试的查询是:

public function getThreads() {
    return $this->createQueryBuilder('m')
        ->select('DISTINCT m.thread')
        ->where('m.thread IS NOT NULL')
        ->orderBy('m.thread', 'DESC')
        ->setMaxResults(10)
        ->getQuery()
        ->getResult();

消息实体:

class Message
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Ad", inversedBy="messages")
 * @ORM\JoinColumn(nullable=true)
 */
private $ad;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Message")
 */
private $thread;
.....

公平地说,我确实设法让它与 DQL 一起工作,但是,你知道,我似乎无法让它不使用查询生成器解决。

顺便说一下,这里是 DQL:

    public function getThreads() {
    $query = $this->em->createQuery(
        'SELECT DISTINCT(m.thread) FROM App:Message m 
        WHERE m.thread IS NOT NULL 
        ORDER BY m.thread DESC
        LIMIT 10 ');
    return $query->getResult(); 
}

谢谢

【问题讨论】:

  • 您是否尝试过按照Doctrine's QueryBuilder documentation 的示例使用QueryBuilder 的->distinct() 方法:$qb = $em->createQueryBuilder() ->select('u') ->distinct() ->from('User', 'u');
  • 我试过了,像这样:`$this->createQueryBuilder('m') ->select('m.thread') ->distinct()`,但它抛出了同样的错误。此外,在这样做时,没有地方可以告诉学说哪一列应该是不同的。
  • 哎呀...我的立场是正确的。与我在select('m.thread) 上面的评论中所说的相反,它告诉学说哪一列应该是不同的。不过没关系,错误依然存在。

标签: php mysql doctrine-orm symfony4


【解决方案1】:

尝试其中一种解决方案,我认为您的问题是您没有使用查询生成器在解决方案中指定“发件人”。您也可以从 createQueryBuilder() 函数中删除“m”,该函数不接收任何参数。我希望其中一种解决方案对您有用。

解决方案 1

public function getThreads(){
    return $this->em->createQueryBuilder()
                    ->select('DISTINCT m.thread')
                    ->from('App\Entity\Message', 'm')
                    ->where('m.thread IS NOT NULL')
                    ->orderBy('m.thread', 'DESC')
                    ->setMaxResults(10)
                    ->getQuery()
                    ->getResult();
}

解决方案 2

public function getThreads(){
    return $this->em->createQueryBuilder()
                    ->select('m.thread')->distinct()
                    ->from('App\Entity\Message', 'm')
                    ->where('m.thread IS NOT NULL')
                    ->orderBy('m.thread', 'DESC')
                    ->setMaxResults(10)
                    ->getQuery()
                    ->getResult();
}

解决方案 3

public function getThreads(){
    $queryBuilder = $this->em->createQueryBuilder();
    $queryBuilder->select('m.thread')->distinct()
                 ->from('App\Entity\Message', 'm')
                 ->where($queryBuilder->expr()->isNotNull('m.thread'))
                 ->orderBy('m.thread', 'DESC')
                 ->setMaxResults(10);

    $query = $queryBuilder->getQuery();
    $result = $query->getResult();
    return $result;
}

【讨论】:

  • 看来我的 Symfony/Doctrine 应用程序不同意您关于 createQueryBuilder() function 不需要任何参数的说法。我已经用->from() 子句尝试过。它抛出错误Too few arguments to function Doctrine\ORM\EntityRepository::createQueryBuilder(), 0 passed in .../Repository/MessageRepository.php on line 115 and at least 1 expected。第 115 行,您可以想象是上面第一个解决方案中的 return $this->createQueryBuilder()
  • 你使用的是什么版本的 Doctrine?
  • 对不起,我的错,我忘了添加 $em 变量(实体管理器)对象。我将通过此更改更新答案。
  • @BernardA,在我添加了“em”变量之后,它对你有用吗?
  • 抱歉,耽搁了一段时间。即使我将$this->em 添加到查询中,恐怕错误仍然存​​在。不知道发生了什么。
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2016-04-02
  • 1970-01-01
  • 2015-07-03
  • 1970-01-01
相关资源
最近更新 更多