Limiting list of items in actionAdmin

I want to limit the list of available items in my actionAdmin view, but I am not sure how. In the controller I have the pretty standard


$model=new Patient('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Patient']))

			$model->attributes=$_GET['Patient'];


		$this->render('admin',array(

			'model'=>$model,

		));

I want to limit the Patient models available based on some other table relationships. It’s a bit complicated, and I’m prepared to use SQL to do it - but where do I do it?

Based on the User logged in, they should only see Patients that are active in Studies of which the User is a managing member.

User->Group->Study->Patient

I can handle the relations, but I don’t understand where to start limiting in the actionAdmin…

If you want to limit though the table the grid displays just use the controller with something like:


$model = new Model('search'); 

$model->attribute= 'value'; // place after the previous line

If you want to limit through a relation you can you the search method in your model and define the criteria there. Basic example:


$criteria->compare('relation.Attribute',$this->definedVariable);

$criteria->with =  array(    

                      'relation2' => array(

			'condition'=>'relation2.Attribute=:att', 

			'params'=>array(':att'=>'value')),

		        'joinType'=>'INNER JOIN',

		        ),

		   );

$criteria->together=true;

Hmm…I tried the second method, but it’s not doing anything. its like it’s not even there. My relation is a multiple though, its an array, maybe that’s causing the problem? How can I apply if it’s a multiple relation?

What was your code? Did you do something like this?


'condition'=>'relation2.relation3.Attribute=:att'

You can also write out the SQL (i.e. something like INNER JOIN statement or whatever suits your needs) if the Join is very complex. I tend to keep to using AR though as it is pretty robust.

The example I gave above is from an actual working app. I can probably dig up a better example this weekend of something I have done that is a lot more complex.

Just in case you haven’t been looking at this: Relational Active Record

Im probably going to fall back on some SQL. For now. Thanks for helping!

edit: whoa. didn’t know about the THROUGH operator/relation. Wow. Thank you! (I’m still very very new to yii)