[Zend] Selecting ambiguous column names

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

[Zend] Selecting ambiguous column names

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: [Zend Framework] Selecting ambiguous column names

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: [Zend Framework] Selecting ambiguous column names

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: [Zend Framework] Selecting ambiguous column names

Post by josh »

Just subclass & override protected function _tableCols() on Zend_Db_Select

Also relevant, describeTable() on Zend_Db_Adapter
Post Reply