Details
Description
Expected behaviour - Using boolean types in Query Builder
The following works
$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); $qb ->select('entity') ->from('FooBundle:Entity', 'entity') ->where($qb->expr()->eq('entity.visible', true)) ; $query = $qb->getQuery(); return $query->getResult();
The following throws an Doctrine/ORM/Query/QueryException error [Syntax Error] line 0, col -1: Error: Expected Literal, got end of string.
$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); $qb ->select('entity') ->from('FooBundle:Entity', 'entity') ->where($qb->expr()->eq('entity.visible', false)) ; $query = $qb->getQuery(); return $query->getResult();
This is due to Doctrine\ORM\Query\Expr\Comparison::_toString()
public function __toString() { return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr; }
When $this->_rightExpr === true type coercion results in 1, but for false it results in an empty string, thus
resulting in invalid SQL.
The following in Doctrine\ORM\Query\Expr\Comparison::__construct() resolves the problem (https://github.com/doctrine/doctrine2/pull/297)
public function __construct($leftExpr, $operator, $rightExpr) { $this->_leftExpr = $leftExpr; $this->_operator = $operator; $this->_rightExpr = $rightExpr === false ? (int) $rightExpr : $rightExpr; }
Could be related to http://www.doctrine-project.org/jira/browse/DDC-1048 and http://www.doctrine-project.org/jira/browse/DDC-949
Fixed, but your code is wrong. It has to be
$qb->expr()->literal(true);