Page 1 of 1

[Zend] Selecting ambiguous column names

Posted: Sat Oct 23, 2010 1:49 pm
by John Cartwright
When working with the Zend_Db, does anyone know if there is a way to select ambigious column names between tables, without having to explicitely use alias or prioritize column selection.

I.e.,

SELECT *
FROM table1
INNER JOIN table2 ON table1.fkid =table2.pkid

If both columns had a "foobar", the latter table would be the one to return it's foobar column value. Preferably, what I would like to see is, when I pass it a wildcard, it would do some magic and auto expand to, lets say

SELECT table1.id, table1.foobar
table2.id, table2.foobar
...

and return the values grouped by their table.

Now I realize this is probably a design decision, however, having come from CakePHP, and this is how they do it, I would like to expand this functionality into ZF.

Any ideas?

Re: [Zend Framework] Selecting ambiguous column names

Posted: Sat Oct 23, 2010 2:19 pm
by Eran
There is no way to do this magically in ZF. It does split the wildcards per each table in the query, so you get table1.*,table2.* and so forth, so you can specify the columns for only one of the tables if you really need it.
As a general principle, I try to avoid wildcards and select columns by their names.

Re: [Zend Framework] Selecting ambiguous column names

Posted: Sat Oct 23, 2010 2:23 pm
by John Cartwright
Eran wrote:There is no way to do this magically in ZF. It does split the wildcards per each table in the query, so you get table1.*,table2.* and so forth, so you can specify the columns for only one of the tables if you really need it.
As a general principle, I try to avoid wildcards and select columns by their names.
I figured so. I haven't actually dug into the ZF code in quite some time, but I expect I should be able to override sometime to provide it meta data about the tables in question -- but what exactly is still to be determined.

Mostly just curious if others have dealt with this as I've been working around this for quite some time. It's just annoiying when you have to split out 30 odd custom alias in a query.

Re: [Zend Framework] Selecting ambiguous column names

Posted: Sat Oct 23, 2010 7:22 pm
by josh
Just subclass & override protected function _tableCols() on Zend_Db_Select

Also relevant, describeTable() on Zend_Db_Adapter