Issues

ZF-6721: Bug if Join is called with an non-associative array for tablename

Description

While using ZF 1.7.6, I found this bug. It seems still present in 1.8.1


protected function _join($type, $name, $cond, $cols, $schema = null)

Exctracting code from ZF 1.8.1, this function cannot be called with a non-associative array for $name parameter because :



757        if (empty($name)) {
758            $correlationName = $tableName = '';
759        } else if (is_array($name)) {
760            // Must be array($correlationName => $tableName) or array($ident, ...)
761            foreach ($name as $_correlationName => $_tableName) {
762                if (is_string($_correlationName)) {
763                    // We assume the key is the correlation name and value is the table name
764                    $tableName = $_tableName;
765                    $correlationName = $_correlationName;
766                } else {
767                    // We assume just an array of identifiers, with no correlation name
768                    $tableName = $name;
769                    $correlationName = $this->_uniqueCorrelation($tableName);
770                }
771                break;
772            }
773        } else if ($name instanceof Zend_Db_Expr|| $name instanceof Zend_Db_Select) {
774            $tableName = $name;
775            $correlationName = $this->_uniqueCorrelation('t');
776        } else if (preg_match('/^(.+)\s+AS\s+(.+)$/i', $name, $m)) {
777            $tableName = $m[1];
778            $correlationName = $m[2];
779        } else {
780            $tableName = $name;
781            $correlationName = $this->_uniqueCorrelation($tableName);
782        }
783
784        // Schema from table name overrides schema argument
785        if (!is_object($tableName) && false !== strpos($tableName, '.')) {
786            list($schema, $tableName) = explode('.', $tableName);
787        }

As you can easily see, if :


$name = array('test');

The code pass over line 759 then, in foreach do only once the loop, going in else part (line 768). I think there is at least a bug on this line 768. Correct code should be :


768                    $tableName = $_tableName;

Then maybe other lines should change after, I don't know... But I'm sure of this bug because if you continue to follow the PHP code, you see that the next use of $tableName is on line 785. And at this point, $tableName contains array('test'). So is_object($tableName) is false, then strpos($tableName, '.') raise an error, because $tableName is not a string, but an array.

Hope solving this issue will help to solve other problems related to JOIN's feature.

Comments

Thank You for Your information. It helps me BIG! :-) Solved in SVN r15843