Limiting data in CGridView using findAllByAttributes?

Hi all, I am new to Yii, and PHP frameworks in general. I have been working with Yii for a little over 2 weeks. After searching the forums and wiki, I feel like I am at a dead end.

I am calling CGridView in my “index” view file. It works fine, but I would like to limit the records in the table based on custom criteria. Basically, based on who is logged in, I would like to serve up a limited version of the whole table. The data will be restricted by the entry in the ‘dept’ column, where some users might see the records from deptX and deptY, while others will only be able to see deptX records, etc.

In trying to do this, I have added to the public function actionIndex() in the Controller file.




public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('ProjectList');

		

		$model=new ProjectList('search');

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

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

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

			

		// This serves up different content based on who is logged in.

		switch (Yii::app()->user->name) {

			// If user 'test1' is logged in, only show records where dept is 'deptX'

			case 'test1':

				$filterModel=ProjectList::model()->findAllByAttributes(array('dept'=>'deptX'));

				if($filterModel==null)

					echo 'Warning message';

				else {

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

						'model'=>$filterModel,

					));

				}

				break;


			// If user 'test2' is logged in, only show records where dept is 'deptY'			

			case 'test2':

				$filterModel=ProjectList::model()->findAllByAttributes(array('dept'=>'deptY'));

				if($filterModel==null)

					echo 'Warning message';

				else {

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

						'model'=>$filterModel,

					));

				}

				break;

			


			// By default, show all records


			default: 

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

					'model'=>$filterModel,

				));

				break;

		}

	}



When logged in as ‘test1’, I get the following error:

[size="3"][b]Error 500

get_class() expects parameter 1 to be object, array given[/b][/size]

When I do a var_dump on the model variable, I see the results I need. I just need to be able to hand that off to CGridView, if possible.

There is probably an extremely simple way to limit the data and serve it to the view, and I am just missing some crucial concept. I apologize in advance for this simplistic question. Thanks for taking the time to read it over, and for helping a total newbie.

Welcome to the forum.

If you use CGridView the same way as in the admin view, just one model instance should be passed to the view, initialized with filter values for later comparison in the models search() method.

In your case you should not perform a findAll query in the controller (which will return an array of AR objects), instead you should just specify the filter value in the filterModel instance




$filterModel = new ProjectList;

...

$filterModel->dept = 'deptX';



(untested)

/Tommy

Tommy,

Thank you so much for your reply! This is exactly what I needed, and it’s working now! I had no idea how to write what I needed. I look forward to learning more about Yii.