Doctrine2 Criteria()生成错误的MySQL查询

时间:2013-08-06 06:46:45

标签: mysql doctrine-orm zend-framework2 criteria

当我使用 \ Doctrine \ Common \ Collections \ Criteria :: create()

use Doctrine\Common\Collections\Criteria;
...
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('isPublished', 1))
        ->andWhere(Criteria::expr()->eq('isDeleted', 0));

$this->comments->matching($criteria)

我收到错误:

Message:
An exception occurred while executing 'SELECT t0.id AS id1, t0.rating AS rating2, t0.text AS text3, t0.username AS username4, t0.isPublished AS isPublished5, t0.isDeleted AS isDeleted6, t0.dateCreated AS dateCreated7, t0.userIP AS userIP8, t0.user_id AS user_id9, t0.product_id AS product_id10 FROM product_comments t0 WHERE ((t0.isPublished IS ? AND t0.isDeleted IS ?) AND t0.product_id IS ?)' with params {"1":1,"2":0,"3":1123}:

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 '1 AND t0.isDeleted IS 0) AND t0.product_id*IS 1123)' at line 1

问题是操作数'IS'在哪里clausule。它不是MySQL的操作数。 (如果我将此查询粘贴到MySQL终端,并更改“IS”=>“=” - 就可以了) 为什么Doctrine会这样的查询呢?问题出在哪儿?

3 个答案:

答案 0 :(得分:1)

我解决了更改Doctrine \ ORM \ Persisters \ BasicEntityPersister的第91行

Comparison::IS  => 'IS %s',

Comparison::IS  => '= %s',

答案 1 :(得分:1)

这是通过将Doctrine ORM升级到2.3.5或更高版本而修复的原则中的错误。 http://www.doctrine-project.org/jira/browse/DDC-2471上的错误报告 关于这个问题的更多讨论 https://github.com/doctrine/collections/commit/3db3ab843ff76774bee4679d4cb3a10cffb0a935#diff-757942c669bf6be9910786b2558ad745

答案 2 :(得分:-1)

尝试替换

  expr()->eq('isPublished', 1)  and expr()->eq('isPublished', 0) with

 expr()->eq('isPublished', '?1')
 expr()->eq('isPublished', '?0')