Using Relationships With 'on' Conditions

I have a model with a relationship defined:




'notes' => array(self::HAS_MANY, 'Note', '','on'=>"notes.owner='MyClass' and notes.owner_id=t.id")



If I use eager loading then I can access all the notes. Eg:




$result1 = Problem::model()->with('notes')->findByPk(1);

echo count($result1->notes) . " notes found";



However if I try and use the lazy approach to avoid unnecessary database processing, it will fail:




$result2 = Problem::model()->findByPk(1);

echo count($result2->notes) . " notes found";



Produces an SQL error when trying to lazily access $result2->notes, because the underlying SQL statement doesn’t seem to know about the table alias ‘t’:




Column not found: 1054 Unknown column 't.id' in 'where clause'.

The SQL statement executed was: SELECT `notes`.`id` AS `t1_c0`, `notes`.`owner` AS `t1_c1`, `notes`.`owner_id`

AS `t1_c2`, `notes`.`note` AS `t1_c3` ... FROM `tbl_note` `notes` WHERE (notes.owner='MyClass' and notes.owner_id=t.id) 



Can anyone explain this? Is it a ‘feature’ of the framework or am I messing up?

On lazy loading there is no main table as you can see. Why didn’t you specify a foreign key? Maybe it will help:




'notes' => array(self::HAS_MANY, 'Note', 'owner_id', 'on'=>"notes.owner='MyClass'")



I’d missed that the option was available in this case. But it does work doing that, so thanks. :)