Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1683

Doctrine\ORM\Query\Expr\Comparison Doesn't handle boolean values properly

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.1.6, 2.2
    • Fix Version/s: 2.1.7, 2.2.2
    • Component/s: DQL, ORM
    • Security Level: All
    • 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

        Activity

        Hide
        Benjamin Eberlei added a comment -

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

        $qb->expr()->literal(true);
        
        Show
        Benjamin Eberlei added a comment - Fixed, but your code is wrong. It has to be $qb->expr()->literal( true );

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Peter Mitchell
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: