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

        Peter Mitchell created issue -
        Peter Mitchell made changes -
        Field Original Value New Value
        Description Expected behaviour - Using boolean types in Query Builder

        The following works

        {code}
        $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();
        {code}

        The following throws an {{ Doctrine/ORM/Query/QueryException }} error {{ [Syntax Error] line 0, col -1: Error: Expected Literal, got end of string. }}

        {code}
        $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();
        {code}

        This is due to {{ Doctrine\ORM\Query\Expr\Comparison::_toString() }}

        {code}
        public function __toString()
        {
            return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr;
        }
        {code}

        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 resolves the problem
        {code}
        public function __construct($leftExpr, $operator, $rightExpr)
        {
            $this->_leftExpr = $leftExpr;
            $this->_operator = $operator;
            $this->_rightExpr = $rightExpr === false ? (int) $rightExpr : $rightExpr;
        }
        {code}

        Could be related to http://www.doctrine-project.org/jira/browse/DDC-1048 and http://www.doctrine-project.org/jira/browse/DDC-949
        Expected behaviour - Using boolean types in Query Builder

        The following works

        {code}
        $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();
        {code}

        The following throws an {{Doctrine/ORM/Query/QueryException}} error {{[Syntax Error] line 0, col -1: Error: Expected Literal, got end of string.}}

        {code}
        $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();
        {code}

        This is due to {{Doctrine\ORM\Query\Expr\Comparison::_toString()}}

        {code}
        public function __toString()
        {
            return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr;
        }
        {code}

        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
        {code}
        public function __construct($leftExpr, $operator, $rightExpr)
        {
            $this->_leftExpr = $leftExpr;
            $this->_operator = $operator;
            $this->_rightExpr = $rightExpr === false ? (int) $rightExpr : $rightExpr;
        }
        {code}

        Could be related to http://www.doctrine-project.org/jira/browse/DDC-1048 and http://www.doctrine-project.org/jira/browse/DDC-949
        Peter Mitchell made changes -
        Description Expected behaviour - Using boolean types in Query Builder

        The following works

        {code}
        $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();
        {code}

        The following throws an {{Doctrine/ORM/Query/QueryException}} error {{[Syntax Error] line 0, col -1: Error: Expected Literal, got end of string.}}

        {code}
        $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();
        {code}

        This is due to {{Doctrine\ORM\Query\Expr\Comparison::_toString()}}

        {code}
        public function __toString()
        {
            return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr;
        }
        {code}

        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
        {code}
        public function __construct($leftExpr, $operator, $rightExpr)
        {
            $this->_leftExpr = $leftExpr;
            $this->_operator = $operator;
            $this->_rightExpr = $rightExpr === false ? (int) $rightExpr : $rightExpr;
        }
        {code}

        Could be related to http://www.doctrine-project.org/jira/browse/DDC-1048 and http://www.doctrine-project.org/jira/browse/DDC-949
        Expected behaviour - Using boolean types in Query Builder

        The following works

        {code}
        $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();
        {code}

        The following throws an {{Doctrine/ORM/Query/QueryException}} error {{[Syntax Error] line 0, col -1: Error: Expected Literal, got end of string.}}

        {code}
        $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();
        {code}

        This is due to {{Doctrine\ORM\Query\Expr\Comparison::_toString()}}

        {code}
        public function __toString()
        {
            return $this->_leftExpr . ' ' . $this->_operator . ' ' . $this->_rightExpr;
        }
        {code}

        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)
        {code}
        public function __construct($leftExpr, $operator, $rightExpr)
        {
            $this->_leftExpr = $leftExpr;
            $this->_operator = $operator;
            $this->_rightExpr = $rightExpr === false ? (int) $rightExpr : $rightExpr;
        }
        {code}


        Could be related to http://www.doctrine-project.org/jira/browse/DDC-1048 and http://www.doctrine-project.org/jira/browse/DDC-949
        Benjamin Eberlei made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.1.7 [ 10198 ]
        Fix Version/s 2.2.2 [ 10195 ]
        Resolution Fixed [ 1 ]
        Benjamin Eberlei made changes -
        Workflow jira [ 13498 ] jira-feedback [ 15257 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback [ 15257 ] jira-feedback2 [ 17121 ]
        Benjamin Eberlei made changes -
        Workflow jira-feedback2 [ 17121 ] jira-feedback3 [ 19374 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved: