Using Findallbyattributes() With Array Parameter

I’ve tried searching for awhile on how to do this but I can’t seem to find anything on it

I’m fairly new to Yii and I’m trying to use findAllByAttributes to get models based on an array of ids

I need to get back report models that match multiple user ids retrieved in another step

the way I’ve tried is:

$model = Report::model()->findAllByAttributes(array(‘user_id’=>$reports));

where $reports is an array of user_ids generated in a foreach loop

however it doesn’t seem to work.

Any ideas?

Hi jhsbutta, welcome to the forum.

I think it should work according to the reference of findAllByAttributes.

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

The problem might be in $reports array. How do you generate it?

Thank you,

I ended up getting it to work, but not with findAllByAttributes, instead I did this:

$model = new Report(‘search’);

		$model->unsetAttributes();


		$model->setAttribute('user_id',$reports);

again, where $reports was the array of user_ids

I’m not entirely certain why findAllByAttributes wasn’t working, whenever I passed $model to the view it seemed to cause it to bug out and only show the title of the view.

Ah, so you are working with a CGridView (or CListView) and a CActiveDataProvider …




$model = Report::model()->findAllByAttributes(array('user_id'=>$reports));



$model in the above is an array of Reports retrieved from the database.

(You’d be better write $models = …)




$model = new Report('search');

$model->unsetAttributes();

$model->setAttribute('user_id',$reports);



But $model in the above is a single object of Report which conveys the search parameters.

They are the different things and CGridView requires the latter to get the data provider.

This wiki will help you understand how things are working together.

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/