Hi,
I’ve problems managing AR relations involving the same table, in particular using hierarchies and the single table inheritance pattern.
Consider, for example, a table Person with three columns: id, marriedWithId, name, gender.
I have to create two AR classes, Male and Female, that are mapped both to Person table.
I have to redefine default scope of both classes (example for class Female):
public function defaultScope()
{
return array('condition' => $this->tableName().".gender='female'");
}
This allows things like Female::model()->findAll() to effectively return just rows with the column "gender" set to "female".
Then consider the relation "wife" defined in the Male class:
'wife' => array(self::BELONGS_TO, 'Female', 'marriedWithId')
Now, I can load a Male named ‘Homer’
$homer = Male::model()->findByAttribute(array('name' => 'Homer'))
Now, if I try to load homer’s wife, I will get in every case no results:
$marge = $homer->wife;
$marge is always null!!!!
This is because during the query there will be a join between the same table, Person, and the condition gender=‘female’ is thus applied on both left and right side of the join, because no disambiguation on columns name is apparently possible.
Is there a way to resolve this matter!?!?!?
Thanks everybody.
Regards,
Mario.