Issues

ZF-5830: Zend_Db_Table_Select doesn't allow use of $select->columns('..')

Description

Zend_Db_Table_Select doesn't allow use of $select->columns('..')

code fragment: $tbl = new Category_Table(); $select = $tbl->select()->columns('id');

Results: Zend_Db_Select_Exception: No table has been specified for the FROM clause in /usr/share/php/Zend-1.7.5/Db/Select.php on line 224

Expected: Zend_Db_Table_Select should already have a table when created from a Zend_Db_Table_Abstract.

Bug?: Zend_Db_Table_Select's from clause isn't set till assemble()

Comments

Agreed.

I already told Zend (ralph probably) that a Zend_Db_Table_Select should be given it's own table in the from clause, just after creating it. It's not logic to have a Zend_Db_Table_Select entirely empty at creation, it should at least contains its own table as its first from clause.

See :


// that's not logic
$table->fetchall($table->select()->from($table, array('my', 'cols')));

// that seems more logic
$table->fetchall($table->select()->columns(array('my', 'cols')));

Turns out just adding the from in the setTable doesn't act as expected.

$select = $tbl->select()->columns('id'); would still include an all columns in the select.

'select table.* , table.id from table'

Attached is a patch I used to fix the bug for me.

short version: Added a different version of the columns method for the class that includes the 'from' on demand. If a column not from the primary table is requested, it will throw an error. Seemed logical since the assemble for Zend_Db_Table_Select would throw for any columns not in the primary table.

This issue is already known as ZF-3239. It's a tricky one and hard solve without BC issues. A real patch probably won't be available until ZF 2.0, where we can break the BC a bit. Although I would love to be proven wrong in this case ;)

Since 1.9 there has been a feature in place that allowed select() to be called on a table with that will load the from part into the object.

Please see here, and the api doc here

I think it might fix this problem.

-ralph

Was fixed as of 1.9. Marking issue closed.

I got this error, today on Version 1.10.6.

Got the same error today. Version 1.11.5

Also using Version 1.11.5, replicated the issue using this code:

public function fetchThumbnailBarContents()
{
    $table = $this->getTable();
    $select = $table->select()->columns('index_image')->where('status=1')->order('created')->limit(9);
    return $table->fetchAll($select);
}

and:

public function getTable()
{
    if (null === $this->_table) {
        require_once APPLICATION_PATH . '/models/DbTable/IndexImages.php';
        $this->_table = new Model_DbTable_IndexImages;
    }
    return $this->_table;
}

All other pulls from the same pattern work fine, but when I try to specify just the index_images column I get error message:

Message: No table has been specified for the FROM clause

Luckily enough, it's a lightweight table and I was just doing minor performance tuning, but it would be nice if this could be resolved.