Whole Gridview Table In Another Model - Filters

I have a model called Person_Event , in which I’m showing the CgridView of the model Person and the column filters are not working.

I tried to use “related model filters” (http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/) but it doesn’t worked since i don’t show several columns in another model’s grid , but the whole grid of the model Person.

In order to be able to show the grid from the beginning I had to change those 2 lines:


'dataProvider'=>Person::model()->search(),

	'filter'=>Person::model(),

from $model to Person::model().

I thought that it is enough and that the search of the Person model will be called , but it isn’t.

Any ideas why?

You can define the filter for each column.

Below is the code I have:




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'employee-form-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'employee_id',

		array(

			'name'=>'is_agent',

			'filter'=>EnumRadioButtonHtml::enumItem($model,'is_agent'),

		),

		//'reference_number',

		array(

			'name'=>'reference_number',

			'value'=>'$data->reference_number',

			'filter'=>CHtml::listData($model->findAllBySql('SELECT reference_number

				FROM employee

				GROUP BY reference_number ORDER BY reference_number'), 'reference_number', 'reference_number'),

		),

		'access_code',

...



I have 2 columns where I put a dropDown filter. The filter input is working well , but the rows are not being filtered , so I guess it doesn’t come to the Person’s search method. . .

Other suggestions?

Then check your Person::model()->search() code, and see if you did compare the correct filter inputted.

This is the Person’s search function. I marked by arrows the 4 fields I want to filter:


	public function search($param = array())

	{

		$criteria=new CDbCriteria($param);


		$criteria->compare('id',$this->id);

		$criteria->compare('tz',$this->tz,true);

		$criteria->compare('first_name',$this->first_name,true); //<-------

		$criteria->compare('last_name',$this->last_name,true); //<-------

		$criteria->compare('birth_date',$this->birth_date,true);

		$criteria->compare('age',$this->age);

		$criteria->compare('city',$this->city,true);

		$criteria->compare('address',$this->address,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('phone',$this->phone,true);

		$criteria->compare('level_id',$this->level_id);                 //<------

		$criteria->compare('broughtBy_id',$this->broughtBy_id);

		$criteria->compare('status_id',$this->status_id, true);

		$criteria->compare('create_time',$this->create_time,true);//<--------

		$criteria->compare('create_user_id',$this->create_user_id);

		$criteria->compare('update_time',$this->update_time,true);

		$criteria->compare('update_user_id',$this->update_user_id);

		$criteria->compare('rating',$this->rating);

		$criteria->compare('newsletter',$this->newsletter);

		$criteria->compare('facebook_update',$this->facebook_update);

		

		$criteria->compare('tblJobs.type',$this->job_search,true);

                $criteria->with=array('tblJobs');

				$criteria->group='t.id';

                $criteria->together=true;

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

			'pagination'=>array('pageSize'=>10),

			

				    'sort'=>array(             

        'attributes'=>array(

           

			'job_search'=>array(

                'asc'=>'tblJobs.type',

                'desc'=>'tblJobs.type DESC',

            ),

			

            '*',

        ),

    ),

		));

	}

And this is person’s CgridView which is in the _form of Person_Event model:


<?php $this->widget('ext.selgridview.SelGridView', array(

	'id'=>'person-grid',

	'selectableRows' =>2,

	'dataProvider'=>Person::model()->search(array('join'=>'INNER JOIN tbl_person_job ON tbl_person_job.person_id=id AND tbl_person_job.job_id=1','condition'=>'status_id="active"')),

	'filter'=>Person::model(),

	'columns'=>array(

		array(        

				'id' => 'selectedIds',

				'class'=>'CCheckBoxColumn',

			),

		array(   

            'header'=>'num',

            'value'=>'$row+1',

			'htmlOptions'=>array('style' => 'text-align: center;'),

        ),

		array(

			'name'=>'first_name',

			'htmlOptions'=>array('style' => 'text-align: center;'),	

		),

		array(

			'name'=>'last_name',

			'htmlOptions'=>array('style' => 'text-align: center;'),	

		),

		array(        

			'name'=>'level_id',

		    'value'=>'$data->getLevelText()',

			'htmlOptions'=>array('style' => 'text-align: center;'),

			'filter'=>CHtml::listData(Person::model()->findAll(array('order'=>'level_id DESC')),'level_id','level_id'),

		),	

		array(

			'name'=>'create_time',

			'htmlOptions'=>array('style' => 'text-align: center;'),	

		),

	),

)); ?>

So far - no success. . . :(

Any ideas what am I missing?

I’m also have the same problem… :-[

please help us !

you can remove join tbl_person_job table by relationship, then use with() in your CDbCriteria,




'dataProvider'=>Person::model()->search(array('join'=>'INNER JOIN tbl_person_job ON tbl_person_job.person_id=id AND tbl_person_job.job_id=1','condition'=>'status_id="active"')),



because you defined




$criteria->together=true;



When $criteria->together is set true, only a single SQL will be executed for a relational AR query, even if the primary table is limited and the relationship between a foreign table and the primary table is many-to-one.

And also if you defined inner join, most case it only return the record row valid in both table.

That means each person must have job. Otherwise your search result will return null.

Sometimes, it’s not always code issue, maybe a data issue probably.

Did that. Moreover I later removed the job criteria entirely and set the search back to stock - still the table I got wasn’t reacting to the filters . . . .