ZF-2546: Zend_Db_Table_Select should include its own table in the FROM section

Description

Zend_Db_Table_Select should include its own table name in the FROM section, as it's its goal :) Actually it does not add the actual table to the from clause.

actual code :


    public function setTable(Zend_Db_Table_Abstract $table)
    {
        $this->_adapter = $table->getAdapter();
        $this->_info    = $table->info();
        return $this;
    }

patch could be :


    public function setTable(Zend_Db_Table_Abstract $table)
    {
        $this->_adapter = $table->getAdapter();
        $this->_info    = $table->info();
        $this->from($this->_info[Zend_Db_Table_Abstract::NAME]);
        return $this;
    }

Comments

Patch attached

Hi Julien

I removed it specifically from there as I wanted the ability for the 'FROM' to be applied either by the developer during query creation, or 'just-in-time' before the query is executed. This is handy, as it means that the select object doesn't need to be tied down with a one-to-one relationship with the parent table.

Although this bug is marked as resolved, it actually is the cause of a number of other bugs listed with Zend_Db_Table_Select.

For me, the expected usage is:

 
// Bugs
$bugs = new Bugs();
$bugs->select()->where('something = ?',$id); 
// I want to manipulate the table data - that's why I use the class !
$bugs->getAdapter()->select()->from('Other_Table')->where('whatever = 1'); 
// I don't wnat the "bugs" included? I'll use the adapter's method

The present situation is confusing. I create the class because I want to save time and typing when manipulating THIS BUG TABLE, not because I want it to perform various DB tasks with it !

Agree with Tomas. Using Zend_Db_Table_Select implies you are wanting to select from the given table. If you don't want that, then you should be using Zend_Db_Select instead.

Simon, can you give a reasonable use case where you'd use Zend_Db_Table_Select instead of Zend_Db_Select to select from a table other than the table from which the select came from? This is especially frustrating as the current implementation flips the order of the from and join


$bugs = new Bugs();
$bugs->select()->join('Table2', 'Bugs.id = Table2.bug_id')->where('whatever = 1');

becomes


SELECT `Bugs`.*
FROM `Table2`
INNER JOIN `Bugs`
WHERE whatever = 1

To fix it you have to specify a from clause explicitly.

Would be good if this issue were reopened and fixed.

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

It does, except that you have to explicitly choose it ($table->select(true)), which makes the API look strange.

And you have to do that if you want joins to work :


$table->select()->setIntegrityCheck(false)->join('other_table', "$this->_name.mycol = other_table.itscol", "some-col");

won't work and will complain about "columns mycol not in $this->_name table".

True, but its not something we can change pre 2.0. Also, to make it more readable, one might consider using:

$table->select(Zend_Db_Table::WITH_FROM_PART)->setItegrityCheck()....

I'd suggest adding these notes to the Zend_Db 2.0 pages in ZFDEV wiki

true and right ;-)