Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1211

Expr::literal problem with numeric value 0

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: Git Master
    • Fix Version/s: 2.0.7, 2.1
    • Component/s: DQL
    • Security Level: All
    • Labels:
      None
    • Environment:
      Apache 2.2.16
      PHP 5.3.3
      Ubuntu 11

      Description

      Hi,

      I'm using master branch of Doctrine 2. Including an Expr with a numeric 0 value (not a string):

      $value = 0;
      
      $qb->expr()->gte($field, $qb->expr()->literal($value));
      

      Expr::literal method will return an empty string ('') instead of '0', which causes that this expression be something like:

      entity.myField >=  
      

      instead of:

      entity.miField >= '0''
      

      This makes the query fail. Tracking the issue down I've found this on Expr\Base::add method:

          public function add($arg)
          {
               if ( ! empty($arg) || ($arg instanceof self && $arg->count() > 0)) {
                  // If we decide to keep Expr\Base instances, we can use this check
                  if ( ! is_string($arg)) {
                      $class = get_class($arg);
      
                      if ( ! in_array($class, $this->_allowedClasses)) {
                          throw new \InvalidArgumentException("Expression of type '$class' not allowed in this context.");
                      }
                  }
      
                  $this->_parts[] = $arg;
              }
          }
      

      The problem is that empty function returns true if you pass '0', so a call to Expr\Base::add would end on NOT adding '0' to $this->_parts array. That's why it finally returns ''.

      I wanted to make the fix for this but I'm having issues running the phing build task. Which are the steps to follow to run the tests? Running phing build task I get on the "test" task:

      Could not create task/type: 'nativephpunit'. Make sure that this class has been declared using taskdef / typedef.

      Thanks!

        Activity

        Hide
        Benjamin Eberlei added a comment -

        Assigned to guilherme, i suppose we could change the !empty($arg) to $arg !== null ?

        Show
        Benjamin Eberlei added a comment - Assigned to guilherme, i suppose we could change the !empty($arg) to $arg !== null ?
        Hide
        Benjamin Eberlei added a comment -

        Fixed

        Show
        Benjamin Eberlei added a comment - Fixed

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            Gustavo Falco
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: