Filter By Relation Attribute In Cgridview

Hi,

I have the following models:

Ticket HAS_ONE Client

Client HAS_MANY Tickets

I manage to show Client Firstname and LastName Within Ticket admin view, instead ClientId. But I don´t know how to filter by Cliente Firstname and Client Lastname

I have this on my Ticket admin view:




<?php 

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

    'id'=>'ticketgrid',

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

    'filter'=>$model,

    'columns'=>array(

        'idTicket',

        

		array( 

			'name'=>'clientTicket.firstname', 

			'header'=>'Firstname', 

			'value'=>'$data->ticketClientRelation->firstname', 

		),

		

		array( 

			'name'=>'clientTicket.lastname', 

			'header'=>'Lastname', 

			'value'=>'$data->ticketClientRelation->lastname',

		),

...



I have this on my Ticket model:




class Ticket extends CActiveRecord

{

	public $clientTicket;

        ...  






public function rules()

	{

		...

			array( 'idTicket      , clienteId,clientTicket'  , 'safe', 'on'=>'search' ),

			

		);






public function relations()

	{

		return array(

			'ticketClientRelation   ' => array(self::BELONGS_TO, 'Client', 'clienteId'),

...



you need to create safe variables in your tickets class and then tickets based over these… Also you can need change

search function and add in criteria togather and with(client) fields

Please also note that as your clientId is a single field, you can only filter based on one field i.e. first name or second

you can also achieve filtration based over first name and last name by first finding out all clients with first name or last name and then placing the data in criteria with In method of SQL i.e. where cleintID In()

Thanks PeRoChAk. Can you provide me any example?

Dear Friend

I hope the following is helpful.

1.First create 2 virtual properties in Ticket Model.

2.Make them safe on search.

3.Modify the search method




class Ticket extends CActiveRecord

{

public $filter1;

public $filter2;


//Make them safe on search.

public function rules()

        {

                ...

             array( 'idTicket ,fiter1,filter2,clientTicket', 'safe', 'on'=>'search' ),

                        

                );

        }




public function search()

	{


           $criteria=new CDbCriteria;

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

           $criteria->together=true;

           ..............................................

           $criteria->compare('ticketClientRelation.firstname',$this->filt1,true);

           $criteria->compare('ticketClientRelation.lastname',$this->filt2,true);

           ....................................................

           return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



4.Add the following colums.

admin.php




'columns'=>array(

		'idTicket',

		array(

		'name'=>"filt1",

		'value'=>'$data->ticketClientRelation->firstname', 

		),

		array(

		'name'=>"filt2",

		'value'=>'$data->ticketClientRelation->lastname', 

		),



I hope I helped a bit.

Hi seenivasan!

Thank you very much!! It helps a lot!!

Regards