CGridView search

Hi,

i have two table, one with second relation


	

public function relations()

{

return array(

    'kat'=>array(self::BELONGS_TO, 'Kategorijos', 'kategorijos_id'),

);

}



In CGridView write:


	

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

	'id'=>'paslauga-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'kodas',

		'pavadinimas',

		'intervalas',

                   array(

                   'name'=>'kat.pavadinimas',

                   'value'=>'$data->kat->pavadinimas',

                

                 ),

            )


)); ?>



but not search or sort in columns ‘kat.pavadinimas’ not works. Sort working, when i replace ‘name’=>‘kat.pavadinimas’, to ‘name’ => ‘kat_id’. What is wrong :confused: ?

For sorting on related data check this two threads:

http://www.yiiframework.com/forum/index.php?/topic/8784-cgridview-sort-columns/

http://www.yiiframework.com/forum/index.php?showtopic=8293

Read this, sort ok, but how with search?

Well, you have to do the searching by yourself inside your model->search() function, so what is the problem to add searching for a related attribute in there?

regards

I add in method model->search() this line: $criteria->compare(‘kat.pavadinimas’,$this->kat->pavadinimas, true); but not working :confused:




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


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


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


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


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


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


		$criteria->compare('kat.pavadinimas',$this->kat->pavadinimas, true);


		return new CActiveDataProvider('Paslauga', array(

			'criteria'=>$criteria,

		));

	}



Hmm yes how to do… like I am doing a count so it does not have a column, but it does have a non working search :P any example would be welcome.

I am having the same problem. Can anyone post an example for a search for attributes of related?

add to your code:

$criteria->with = ‘kat’;

Try This Solution

http://www.yiiframework.com/forum/index.php?/topic/9083-solved-search-filter-of-a-relations-field-through-cgridview/

it’s work 4 me

Hi,

Here’s my solution:

in the view I use

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

'id'=&gt;'article-grid',


'dataProvider'=&gt;&#036;model-&gt;search(),


'filter'=&gt;&#036;model,


'columns'=&gt;array(


	'id',


	'nom',


	'prix',


	'definition',


	/*'mark_owner.nom_marque',*/


            


            array(   // display 'author.username' using an expression


                 





                'name'=&gt;'marque_id',            


                'value'=&gt;'&#036;data-&gt;mark_owner-&gt;nom_marque',


                


            ),





	'image',


	array(


		'class'=&gt;'CButtonColumn',


	),


),

)); ?>

marque_id have as value: $data->mark_owner->nom_marque

And in my model I use something like that and its works:

/**

 * @return array relational rules.


 */


public function relations()


{


	// NOTE: you may need to adjust the relation name and the related


	// class name for the relations automatically generated below.


	return array(


                


                [color=&quot;red&quot;] 'mark_owner'=&gt;array(self::BELONGS_TO,'Marque','marque_id'),[/color]


                 'line_items'=&gt;array(self::HAS_MANY,'LineItem','article_id')


                


	);


}





/**


 * @return array customized attribute labels (name=&gt;label)


 */


public function attributeLabels()


{


	return array(


		'id' =&gt; 'ID',


		'nom' =&gt; 'Nom',


		'prix' =&gt; 'Prix',


		'definition' =&gt; 'Definition',


		'marque_id' =&gt; 'Marque',


		'image' =&gt; 'Image',


	);


}


    


    public function behaviors(){


          return array( 'EAdvancedArBehavior' =&gt; array(


                'class' =&gt; 'application.extensions.EAdvancedArBehavior'));


    }





/**


 * Retrieves a list of models based on the current search/filter conditions.


 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.


 */


public function search()


{


	// Warning: Please modify the following code to remove attributes that


	// should not be searched.


           


           //var_dump(&#036;this);


           //exit(0);


        


           //echo 'gggg'.&#036;this-&gt;prix;


           //exit(0);


            


                    /*-&gt;find (


                    array(':nom_marque'=&gt;&#036;this-&gt;marque_id));*/


             


	&#036;criteria=new CDbCriteria;


            &#036;criteria-&gt;with= 'mark_owner';


	&#036;criteria-&gt;compare('id',&#036;this-&gt;id);


	&#036;criteria-&gt;compare('nom',&#036;this-&gt;nom,true);


	&#036;criteria-&gt;compare('prix',&#036;this-&gt;prix);


	&#036;criteria-&gt;compare('definition',&#036;this-&gt;definition,true);


            


            [color=&quot;red&quot;]if(&#33;empty(&#036;_GET['Article']))


                       &#036;criteria-&gt;compare('mark_owner.nom_marque',Marque::model()-&gt;find(array('condition'=&gt;&quot;nom_marque like '%&quot;.&#036;_GET['Article']['marque_id'].&quot;%'&quot;))-&gt;nom_marque, true );[/color]


           	


            &#036;criteria-&gt;compare('image',&#036;this-&gt;image,true);


            &#036;criteria-&gt;compare('image_zoom',&#036;this-&gt;image_zoom,true);


           


	return new CActiveDataProvider(&#036;this, array(


		'criteria'=&gt;&#036;criteria,


	));


}

I think this code will help all.

Roodly