[eluser]webthink[/eluser]
I managed to do both.
The modification to allow for descriptive/intuitive foreign key names turned out to be surprisingly easy.
Just add this code in join() after the conditionals inside the foreach where $inflected gets set.
Code:
foreach ($this->get_fields() as $field)
{
if (stristr($field,$inflected))
{
//remove _id from end of field name
$inflected = substr($field, 0, -3);
}
}
That's it. This allows you to have a foreign key called something like birth_city_id
and get an object that looks something like
field1
field2
birth_city_id
birth_city_name
The changes required for having <b>multiple</b> foreign keys is a bit more involved.
The following replaces everything from
Code:
foreach ($tables as $inflected=>table)
to
Code:
$alias = $this->_class_name . '_' . $table. '_';
inclusive
here it is
Code:
for ($i=0; $i<count($tables); $i++)
{
$table = $tables[$i];
if ( is_object($table) && get_parent_class($table) == 'ActiveRecord' )
{
$parent = $table;
$inflected = $table->_class_name;
$table = $table->_table;
}
else if ( !is_string($inflected) )
{
$inflected = trim(substr($table, 0, -1));
$table = trim($table);
}
if (! array_key_exists($table, $foreign_keys) )
{
$foreign_keys[$table] = array();
foreach ($this->get_fields() as $field)
{
if (stristr($field,$inflected))
{
$foreign_keys[$table][] = $field;
}
}
if (count($foreign_keys[$table]) > 1)
{
for ($j=0; $j < count($foreign_keys[$table]) - 1; $j++)
{
$tables [] = $table;
}
}
$fk_index[$table] = 0;
}
//remove _id from end of field name
$inflected = substr($foreign_keys[$table][$fk_index[$table]], 0, -3);
$fk_index[$table] ++;
// Make an alias for a joined table to prevent collision
$alias = $this->_class_name . '_' . $table. '_'. $fk_index[$table];
...
this will allow you to have foreign keys such as birth_city_id, residence_city_id
and end up with an object that looks like
field1
field2
birth_city_id
birth_city_name
residence_city_id
residence_city_name
These changes preserve the ability to use the regular table_id where appropriate
Hope someone is finds it helpful.