Doctrine 2.1.5 setFirstResult()在MySQL 5.5中产生错误

时间:2012-04-24 19:36:38

标签: php mysql symfony doctrine-orm dql

此查询:

$this->getEntityManager()
        ->createQuery("SELECT k FROM FooBarBundle:Kode k WHERE k.kode LIKE :keyword OR k.nama LIKE :keyword ORDER BY k.kode ASC")
            ->setParameter("keyword", "%$keyword%")
            ->setMaxResults($limit)
            ->setFirstResult($offset)
        ->getResult();

生成以下SQL错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OFFSET 0' at line 1

但如果删除setFirstResult($offset)行,则可以正常工作。是否有任何解决方法,而不是升级整个Doctrine库(补丁可能没问题)?

1 个答案:

答案 0 :(得分:1)

我认为你必须删除

ORDER BY k.kode ASC
从您的查询

并使用如下方法设置它:

$this->getEntityManager()
        ->createQuery("SELECT k FROM FooBarBundle:Kode k WHERE k.kode LIKE :keyword OR k.nama LIKE :keyword")
            ->setParameter("keyword", "%$keyword%")
            ->setMaxResults($limit)
            ->setFirstResult($offset)
            ->orderBy("k.kode")
            ->getResult();