Doctrine LIMIT语法错误?

时间:2011-07-09 15:16:11

标签: mysql doctrine

'[Syntax Error] line 0, col 71: Error: Expected end of string, got 'LIMIT'' 

这是我的代码:

public function getLatestChapters()
    {
        return $this->_em->createQuery('SELECT c, m FROM models\Chapter c JOIN c.Manga m ORDER BY c.CreateDate LIMIT 10')->getResult();
    }

这可能是什么问题?我怎样才能在Doctrine中使用LIMIT?

我正在使用Doctrine 2

2 个答案:

答案 0 :(得分:16)

好像no中有DQL anymore LIMIT / OFFSET。

$qb = $em->createQueryBuilder();
//.. build your query
$q = $qb->getQuery();
$q->setFirstResult($offset);
$q->setMaxResults($limit);
$result = $q->getResult(); 

答案 1 :(得分:3)

我想参与这篇文章,并想告诉别人如果你想在单元测试中使用带有限制的DBAL,你可以使用以下内容:

$client = static::createClient()
$em = $client->getContainer()->get('doctrine')->getManager();
$query = $em->createQuery('WRITE YOUR QUERY HERE');
$query->setFirstResult(0);
$query->setMaxResults(1);
$data = $query->getResult();

在控制器中也可以使用相同的代码并进行一些修改:)

相关问题