2010年5月31日星期一

Re: [fw-db] Duplicate field names in manytomany relationship


On May 31, 2010, at 5:21 AM, Steve Rayner wrote:
How does Zend Framework deal with the duplicated fieldname for id, created_date and modified_date?

There can be only one column by each name in a Zend_Db_Table_Row object, since the object-field interface must be unambiguous.  

  echo $row->id; // which column will this get?

If you have duplicate column names, one will overwrite the other.  There's no way for you to control which one takes priority.

Can i use some form of alias for these field names so that i can distinguish between them in the resulting recordset?

There ought to be a way to use an object of type Zend_Db_Table_Select as an argument to findManyToManyRowset(), so you could customize the columns retrieved by the query, including aliasing columns.  This is possible with other methods like findDependentRowset() and others.

Unfortunately, findManyToManyRowset(), is hard-coded to fetch all columns from the matching table using the * wildcard.  So you can't customize the columns without creating your own subclass of Zend_Db_Table_Row and rewriting that method, which is very complex.

Your only solution seems to be to bypass the Zend_Db_Table interface and run literal SQL queries, writing the joins and column aliases yourself, and executing the query against the Zend_Db_Adapter interface.

I particularly need the id of the record in the joining table, however the data returned by id seems to be that of the component table.

Right; this would be a good reason to name your primary keys something more descriptive in each table, instead of "ID" everywhere.  Using more descriptive names for columns is good practice anyway.

Regards,
Bill Karwin

没有评论: