#1558: Doctrine_Query_Abstract::whereIn method called with empty params leads to
bad DqlQueryPart
-----------------------------------+----------------------------------------
 Reporter:  honza.trtik            |       Owner:  romanb 
     Type:  defect                 |      Status:  new    
 Priority:  minor                  |   Milestone:  Unknown
Component:  Query/Hydration        |     Version:  1.0.2  
 Keywords:  whereIn query          |    Has_test:  0      
 Mystatus:  Pending Core Response  |   Has_patch:  0      
-----------------------------------+----------------------------------------
 Method Doctrine_Query_Abstract::_processWhereIn() returns $this, when
 called with empty array as $params parameter.
 The whole query object is then saved as dql query part which leads to
 error during parsing dql:

 Catchable fatal error: Object of class Doctrine_Query could not be
 converted to string in
 /symfony/plugins/sfDoctrinePlugin/lib/doctrine/Doctrine/Query/Condition.php
 on line 44

 when the $params parameter is empty, no dql query part should be added
 (attached patch)

 Bellow is part of the Doctrine_Query_Abstract code, which is responsible
 for the error:

 {{{
     /**
      * Adds IN condition to the query WHERE part
      *
      * @param string $expr The operand of the IN
      * @param mixed $params An array of parameters or a simple scalar
      * @param boolean $not Whether or not to use NOT in front of IN
      * @return Doctrine_Query
      */
     public function andWhereIn($expr, $params = array(), $not = false)
     {
         if ($this->_hasDqlQueryPart('where')) {
             $this->_addDqlQueryPart('where', 'AND', true);
         }

         return $this->_addDqlQueryPart('where',
 $this->_processWhereIn($expr, $params, $not), true);
     }


     /**
      * Adds IN condition to the query WHERE part
      *
      * @param string $expr The operand of the IN
      * @param mixed $params An array of parameters or a simple scalar
      * @param boolean $not Whether or not to use NOT in front of IN
      * @return Doctrine_Query
      */
     public function orWhereIn($expr, $params = array(), $not = false)
     {
         if ($this->_hasDqlQueryPart('where')) {
             $this->_addDqlQueryPart('where', 'OR', true);
         }

         return $this->_addDqlQueryPart('where',
 $this->_processWhereIn($expr, $params, $not), true);
     }


     /**
      * @nodoc
      */
     protected function _processWhereIn($expr, $params = array(), $not =
 false)
     {
         $params = (array) $params;

         // if there's no params, return (else we'll get a WHERE IN (),
 invalid SQL)
         if ( ! count($params)) {
             return $this;
         }

         $a = array();
         foreach ($params as $k => $value) {
             if ($value instanceof Doctrine_Expression) {
                 $value = $value->getSql();
                 unset($params[$k]);
             } else {
                 $value = '?';
             }
             $a[] = $value;
         }

         $this->_params['where'] = array_merge($this->_params['where'],
 $params);

         return $expr . ($not === true ? ' NOT ':'') . ' IN (' . implode(',
 ', $a) . ')';
     }
 }}}

-- 
Ticket URL: <http://trac.doctrine-project.org/ticket/1558>
Doctrine <http://www.phpdoctrine.org>
PHP Doctrine Object Relational Mapper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"doctrine-svn" group.
 To post to this group, send email to [email protected]
 To unsubscribe from this group, send email to [EMAIL PROTECTED]
 For more options, visit this group at 
http://groups.google.co.uk/group/doctrine-svn?hl=en-GB
-~----------~----~----~----~------~----~------~--~---

Reply via email to