Guillaume Oriol wrote:
> I wonder why the developpers of Zend_Db component choose the class
name
> instead of column name in the reference map of a Zend_Db_Table
object?
I designed and implemented the table-relationships feature. The
answer to your question of "why" is that the reference map is to help
the Zend_Db_Table_Row object to instantiate related Table objects. It
has nothing to do with designing custom SQL queries. You are using
the $_referenceMap in a manner other than what it was designed for.
You could just as easily say, "I want to hammer in a nail. I have a
screwdriver, but it has a tip that isn't flat or blunt, so it's hard
to use for nailing. I wonder why the screwdriver was designed this
way?"
That's not to say that what you are doing is in any way wrong.
Hammering nails is a fine thing to do in many circumstances. It's
just that the $_referenceMap doesn't contain the information to help
you, so it's not the right tool.
> I need to build dynamically my joins in a findBy* method and try to
build them
> from the existing reference map. But to do so, I need the
referenced table name,
> not the table class name. As the $_referenceMap only contains the
table class
> name, I am obliged to instantiate the class just to get its table
name! Is there
> a better way to do so?
The $_referenceMap is simply a PHP associative array. You can add
elements to it and use them in your custom methods.
protected $_referenceMap = array(
'Bug' => array(
'columns' => array('bug_id'),
'refTableClass' => 'Bugs',
'refColumns' => array('bug_id'),
// You can add a custom array element for example:
'guillaume_table_name' => 'bugs_tbl',
)
);
You can use whatever string you want as the array key. The above is
just an example. Zend_Db_Table_Row_Abstract expects some array keys,
that that are documented, but you can create other keys in the same
array too.
Another solution is to name your Table classes the same as the
database tables they represent. This is the default, by the way.
Only if you declare a Table class including the $_name attribute does
it use that name instead of the name of the class.
class bugs extends Zend_Db_Table_Abstract
{
// no need to declare $_name if the class name is the same as the
table name
}
Note that if you do this, your table name should conform to PEAR class
naming conventions. E.g. a class named "bugs_tbl" should be defined
in file bugs/tbl.php.
Regards,
Bill Karwin