Skip to content

Loading…

DDC-1683: Doctrine\ORM\Query\Expr\Comparison Doesn't handle boolean values properly #2329

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user peterjmit:

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 (#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](http://www.doctrine-project.org/jira/browse/DDC-1048) and http://www.doctrine-project.org/jira/browse/[DDC-949](http://www.doctrine-project.org/jira/browse/DDC-949)

@doctrinebot

Comment created by @beberlei:

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

$qb->expr()->literal(true);
@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.1.7 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.