joined tables overwrite the columns

150 Views Asked by At

While using Zend_Db_Tables and Zend_Db_Table_Select with Zend_Db_Adapter_Pdo_Pgsql, I am having a problem using a select query:

$rowset = $db->select()->from(array('a'=>'tablea'))
             ->columns(array('a.a'))
             ->join(array('b'=>'tableb'),'b.id = a.id', array('b.a'))
             ->query()->fetchAll();

Expected contents of rowset: (mainly the keys)

$rowset['a.a'] = "something";
$rowset['b.a'] = "somethingElse";

In the actual content of $rowset, only $rowset['a'] exists, with no way to differentiate between the joined tables.

Any solutions to this?


When just doing a

$q = "SELECT * FROM tablea AS a
JOIN tableb AS b ON a.key = b.fkey";
$dbtable->getAdapter()->fetchAll($q);

I also don't get what I expect.

1

There are 1 best solutions below

0
On BEST ANSWER

In order to differentiate between tables a and b when there are matching column names, you'll want to alias the columns in table b. The join would then look something like:

->join(array('b' => 'tableb'), 'a.id = b.id', array('a_from_b' => 'b.a'))

Please note that the 'ON' condition should be a string, not an array.