Ticket #1558 (closed defect: fixed)

Opened 9 months ago

Last modified 9 months ago

Doctrine_Query_Abstract::whereIn method called with empty params leads to bad DqlQueryPart

Reported by: honza.trtik Owned by: romanb
Priority: minor Milestone: 1.0.4
Component: Query/Hydration Version: 1.0.3
Severity: Keywords: whereIn query
Cc: Has Test: no
Status: Pending Core Response Has Patch: no

Description

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) . ')';
    }

Attachments

patch_doctrine_query_abstract_where_in.patch (0.6 KB) - added by honza.trtik 9 months ago.

Change History

Changed 9 months ago by honza.trtik

Changed 9 months ago by jwage

  • version changed from 1.0.2 to 1.0.3
  • milestone changed from Unknown to 1.0.4

Changed 9 months ago by jwage

  • status changed from new to closed
  • resolution set to fixed

(In [5087]) [1.0, 1.1] fixes #1558 - issue with empty whereIn params

Note: See TracTickets for help on using tickets.