Issues

ZF-28: Zend_Db_Select::_tableCols() won't select table-specific columns

Description

Hello Zend,

in your method "Zend_Db_Select::_tableCols()" it won't select table specific columns. I have create a own Db_Select class thats extends your class and i have rewrite this method. Do you change this in the future or it is new ready?

my rewrited method:


    protected function _tableCols($tbl, $cols) {
        if (is_string($cols)) {
            $cols = explode(',', $cols);
        } else {
            settype($cols, 'array');
        }

        foreach ($cols as $col) {
            $col = trim($col);
            if ($col != '*' && strpos($col, '(')===false) {
                $col = '`'.$col.'`';
            }
            $this->_parts['cols'][] = '`'.$tbl.'`.'.$col;
        }
    }

Comments

Changing fix version to 0.8.0.

The problem is that the generated SQL may include ambiguous references to columns of the same name in more than one table.

The solution is to prefix the table name or correlation name to the columns, so that columns for a given table, or a wildcard "*" for all columns from the table, are retrieved specifically from one table.

The implementation given is not general-purpose:

  • The backticks `` are MySQL-specific identifier delimiters. The Db adapter should provide the correct quoting for identifiers. See ZF-1.

  • Inferring cases from the "*" or "(" characters does not account for special characters that may occur within delimited identifiers. That is, "foo(bar" is a legitimate identifier when delimited.

Recategorize in Zend_Db_Select component.

Fixed in revision 3320.

Now columns in the select-list are prepended by the correlation name.

If you have columns that should not have correlation name prepended, use '' as the table name. For example:


$select->from( '', 'count(*)');

I may introduce a function {{columns()}} for this purpose too.