[DDC-1683] Doctrine\ORM\Query\Expr\Comparison Doesn't handle boolean values properly Created: 06/Mar/12  Updated: 14/Mar/12  Resolved: 14/Mar/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: DQL, ORM
Affects Version/s: 2.1.6, 2.2
Fix Version/s: 2.1.7, 2.2.2
Security Level: All

Type: Bug Priority: Major
Reporter: Peter Mitchell Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
Labels: None
Environment:

Symfony 2.0.11, MySQL, PDO, PHP Version 5.3.5-1ubuntu7.7



 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



 Comments   
Comment by Benjamin Eberlei [ 14/Mar/12 ]

Fixed, but your code is wrong. It has to be

$qb->expr()->literal(true);
Generated at Tue Jul 29 18:53:55 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.