gsusonline gsusonline - 2 years ago 114
PHP Question

Yii filter not working

wondering if anyone can help.

I have a two pages in my Yii application that use the same controller/model, but use different model functions. My initial page is pretty standard 'admin' page that uses the default search() as the model (this works fine - including filtering), but i've subsequently made a secondary view/action which looks at an altered search function - this has an ID passed into it (pulled from URL params). This works fine, but the filters in page don't work which is annoying.

I think it's because in the controller, i'm not passing the ID variable through correctly???

Here's some of my code;


public function searchByEvent($event_id) {
$criteria = new CDbCriteria;

$criteria->compare('id', $this->id, true);
$criteria->compare('event_id', $this->event_id, true);
$criteria->compare('status_id', $this->status_id, true);
$criteria->compare('checkin_status_id', $this->checkin_status_id, true);
$criteria->compare('guest_of_user_id', $this->guest_of_user_id, true);
$criteria->compare('user_id', $this->user_id, true);
$criteria->compare('assign_group', $this->assign_group, true);

$criteria->with = 'user';
$criteria->compare('user.forename', $this->user_forename, true);
$criteria->compare('user.surname', $this->user_surname, true);
$criteria->compare('', $this->user_company, true);
$criteria->order = 'user.surname ASC';
$criteria->condition = "event_id = :event_id";

return new CActiveDataProvider($this, array(
'criteria' => $criteria,


public function actionAdminByEvent() {

$model = new EventAttendees('searchByEvent');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['EventAttendees']))
$model->attributes = $_GET['EventAttendees'];

$this->render('webroot.themes.'.Yii::app()->name.'.views.adminEventAttend.adminByEvent', array(
'model' => $model,


$event_id = Yii::app()->getRequest()->getParam('event_id');
this->widget('booster.widgets.TbGridView', array(
'id' => 'event-attendees-grid',
'dataProvider' => $model->searchByEvent($event_id),
'pager' => array(
'class' => 'booster.widgets.TbPager',
'displayFirstAndLast' => true,
'filter' => $model,
'selectableRows' => 0,
'selectionChanged' => 'function(id){ location.href = "' . $this->createUrl('view') . '/id/"+$.fn.yiiGridView.getSelection(id);}',
'columns' => array(...

Can anyone see where i'm going wrong with the filters, or if im missing a step with search?

Answer Source

If I were to guess, it's because the attributes in your model are not declared as "safe" when it is under the "searchByEvent" scenario. If you used gii and looked at your model's rules method, you would find an entry there that marks all attributes as "safe" when using the "search" scenario. You are using a different scenario called "searchByEvent" in your controller action ($model = new EventAttendees('searchByEvent')) and if you did not create a new rule that flags your attributes as safe under this scenario, then your mass assignment line ($model->attributes = $_GET['EventAttendees']) will fail for any attributes without any scenario-less rules.

If that isn't the case though, another thing you might want to look at is changing the following:

$criteria->condition = "event_id = :event_id";


$criteria->addColumnCondition(array('event_id' => $event_id))

Your params line might be overwriting all the other parameters that were set with all the previous compare statements because you are not merging your new parameters into the current set. Using the proper CDbCriteria methods helps get around this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download