本文介绍了设定LIMIT与教义2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个查询(使用子查询),但我不知道如何在子查询中设置限制。
我的查询:

I trying to write a query (with subquery) but i don't know how set a limit in my subquery.My query:

$query_ids = $this->getEntityManager()
      ->createQuery(
        "SELECT e_.id
        FROM MuzichCoreBundle:Element e_
        WHERE [...]
        GROUP BY e_.id")
     ->setMaxResults(5)
    ;

$query_select = "SELECT e
      FROM MuzichCoreBundle:Element e
      WHERE e.id IN (".$query_ids->getDql().")
      ORDER BY e.created DESC, e.name DESC"
    ;

$query = $this->getEntityManager()
      ->createQuery($query_select)
      ->setParameters($params)
    ;

但是 - > setMaxResults(5)不起作用SQL查询中没有LIMIT。我们可以用doctrine 2做简单的限制吗?

But ->setMaxResults(5) doesn't work. No 'LIMIT' in the SQL query. Can we do simple LIMIT with doctrine 2 ?

推荐答案

$query_ids = $this->getEntityManager()
      ->createQuery(
        "SELECT e_.id
        FROM MuzichCoreBundle:Element e_
        WHERE [...]
        GROUP BY e_.id")
     ->setMaxResults(5)
     ->setMaxResults($limit)
    ;

在第二个查询中,第一个查询的结果应该被传递..

HERE in the second query the result of the first query should be passed ..

$query_select = "SELECT e
      FROM MuzichCoreBundle:Element e
      WHERE e.id IN (".$query_ids->getResult().")
      ORDER BY e.created DESC, e.name DESC"
    ;


$query = $this->getEntityManager()
      ->createQuery($query_select)
      ->setParameters($params)
      ->setMaxResults($limit);
    ;

$resultCollection = $query->getResult();

这篇关于设定LIMIT与教义2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:16