Relational models, eager/lazy loading, and behaviors

I haven’t been able to find any documentation on this.

Scenario is pretty straightforward - I have a relational model between two tables. The foreign model class defines beforeSave and afterFind functions to handle some data manipulation. When I read write records using the class directly it works fine.

What does not work fine are those behaviors when I try to eager or lazy load the foreign record using the model relationship. So if I do:


$x = Primary::model()->with('foreignTable')->findByPk($primaryPk);

…or…


$x = Primary::model()->findByPk($primaryPk);

$y = $x->foreignTable;

…the behavior routines defined by the foreign table’s model are never called. I think I understand why the first (eager) load might not work (CActiveFinder?), but I’m not sure why the 2nd wouldn’t.

How can I accomplish this?

Thanks all

edit: I should mention that the relationship between the tables is defined correctly, and the foreign table’s data is loaded ok.

Try:


Primary::model()->with('foreignTable')->findByAttributes(array('foreignTable.column'=>$searchValue));



http://www.yiiframework.com/doc/api/CActiveRecord#findByAttributes-detail

No, that doesn’t work, the error says:


"Table "primary" does not have a column named "foreignTable.user_id"."

…where in my case, user_id is the foreign key.

Anyone else?