Grabbing Has_Many Related Model Through Instance Variable ($This)

I’m new to Yii and used to Kohana. I have two models: 1. sampleRequests -> (this has many SampleShares) 2. sampleShares

(the following is in my SampleRequest model:

public function relations() {

	return array(


		'sampleShares' => array(self::HAS_MANY, 'SampleShare', 'reagent_id'),


	);


}

In my case I have already instantiated an object (sampleRequest) and I am trying to get a particular sampleShare based on added criteria (a user_id).

In Kohana I could do:

$myShare = $this->sampleShares->where(‘user_id’,’=’,$my_user_id)->find();

In Yii I have tried something similar:

$myShare = $this->sampleShares->find(array(

‘condition’=>‘user_id:user_id’,

‘params’=>array(’:user_id’=>$my_user_id)));

The previous does not work for me however, the following will work:

$myShare = SampleShare::model()->find(array(

‘condition’=>‘sample_id=:id AND user_id=:user_id’,

‘params’=>array(’:id’=>$this->id, ‘:user_id’=>Yii::app()->user->id)));

So my question is, can you grab related models with appended select criteria to the relationships established in Yii (particular with the instance or $this)? (Similar to my Koahana example?) If so how do you do that?

Read the Guide, yo.

Two things that will help you:

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query-without-getting-related-models

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes

I got it. I just had to replace:

$this->sampleShares->find(

with

$this->with(‘sampleShares’)->find(