Find Model In A Model Problem


Presume I have a database with two tables:

Campaigns and Messages

I also have all respective models and relations already tested and working.

Presume one campaign may have many messages but only one message in the campaign is ‘default’.

Now I can select particular campaign using its primary key:

$campaign = Campaigns::model()->findByPk($id);

and I can select all its messages:

$messages = $campaign->messages;

But I do not know how to select only "default" message.

I tried:

$criteria = new CDbCriteria;

$criteria->addCondition('default = 1');

$message = $messages->find($criteria);

But it only throws error:

Call to a member function find() on a non-object

Any idea how to accomplish the task?

Thanks ahead!


The right way is (I found it myself):

$campaign = Campaigns::model()->findByPk($id);

$messages = $campaign->messages(array('condition'=>'messages.default = 1'));

$message = $messages[0];

Anyway - if anybody knows how to find particular message from ‘messages’:

$campaign = Campaigns::model()->findByPk($id);

$messages = $campaign->messages;

I would appreciate!