条令查询生成器select distinct抛出错误

w1jd8yoj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(371)

这应该是直截了当的。我确实在so和其他地方找到了很多关于这个主题的帖子,但它只是抛出了一个错误:
[语义错误]第0行,第18列“来自app\entity\message的线程”附近:错误:pathexpression无效。必须是statefieldpathexpression。
这用于在消息模块上选择不同的线程。我尝试的查询是:

  1. public function getThreads() {
  2. return $this->createQueryBuilder('m')
  3. ->select('DISTINCT m.thread')
  4. ->where('m.thread IS NOT NULL')
  5. ->orderBy('m.thread', 'DESC')
  6. ->setMaxResults(10)
  7. ->getQuery()
  8. ->getResult();

消息实体:

  1. class Message
  2. {
  3. /**
  4. * @ORM\Id()
  5. * @ORM\GeneratedValue()
  6. * @ORM\Column(type="integer")
  7. */
  8. private $id;
  9. /**
  10. * @ORM\ManyToOne(targetEntity="App\Entity\Ad", inversedBy="messages")
  11. * @ORM\JoinColumn(nullable=true)
  12. */
  13. private $ad;
  14. /**
  15. * @ORM\ManyToOne(targetEntity="App\Entity\Message")
  16. */
  17. private $thread;
  18. .....

公平地说,我确实设法使它与dql一起工作,但是,你知道,我似乎不能让它与查询生成器一起解决。
顺便说一下,这里是dql:

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

谢谢

66bbxpm5

66bbxpm51#

尝试这些解决方案之一,我认为您的问题是您没有在使用查询生成器的解决方案中指定“from”。也可以从createquerybuilder()函数中删除“m”,因为该函数不接收任何参数。我希望这些解决办法之一对你有用。
解决方案1

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

解决方案2

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

解决方案3

  1. public function getThreads(){
  2. $queryBuilder = $this->em->createQueryBuilder();
  3. $queryBuilder->select('m.thread')->distinct()
  4. ->from('App\Entity\Message', 'm')
  5. ->where($queryBuilder->expr()->isNotNull('m.thread'))
  6. ->orderBy('m.thread', 'DESC')
  7. ->setMaxResults(10);
  8. $query = $queryBuilder->getQuery();
  9. $result = $query->getResult();
  10. return $result;
  11. }
展开查看全部

相关问题