relation

Encomenda model:


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(

			'cliente' => array(self::BELONGS_TO, 'User', 'customer'),

			'encomendaLinhas' => array(self::HAS_MANY, 'EncomendaLinha', 'encomenda'),

		);

	}


   public function search()

	{

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

		// should not be searched.

                Yii::import('application.modules.admin.models.User');

		$criteria=new CDbCriteria;


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

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

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

		

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

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

                $criteria->compare('paid',$this->confirmed);

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}




can i use this on search??


$criteria->compare('customer',$this->cliente->nome);

because i get null

use this,


$criteria->compare('cliente.customer',$this->cliente->nome,true);

No.That doesnt work.I have made this to view the realation in the CGridView and it works but i dont see the filter for this column(cliente.nome):


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

	'id'=>'encomenda-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'idencomenda',

		'createDate',

		'totalValue',

		'cliente.nome',

		'quantity',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

replace following array with ‘cliente.nome’

[color="#000000"] array[/color][color="#666600"]([/color][color="#000000"]

   		[/color][color=&quot;#008800&quot;]'name'[/color][color=&quot;#666600&quot;]=&gt;[/color][color=&quot;#008800&quot;]'nome'[/color][color=&quot;#666600&quot;],[/color][color=&quot;#000000&quot;]


   		[/color][color=&quot;#008800&quot;]'header'[/color][color=&quot;#666600&quot;]=&gt;[/color][color=&quot;#008800&quot;]'Nome'[/color][color=&quot;#666600&quot;],[/color][color=&quot;#000000&quot;]


   		[/color][color=&quot;#008800&quot;]'value'[/color][color=&quot;#666600&quot;]=&gt;[/color][color=&quot;#008800&quot;]'&#036;data-&gt;cliente-&gt;nome[/color][color=&quot;#008800&quot;]'[/color][color=&quot;#666600&quot;],   [/color]

[color="#666600"] )[/color]

by default filter enable and use ‘filter’=>false to disable filter

on value if a put:


'value' => '$model->cliente->nome',

error:Undefined variable: model

or without quotes like this:


'value' => $model->cliente->nome,

Trying to get property of non-object

i dont understand

Try this:





 public $client_nome;


  public function search()

	{

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

		// should not be searched.

                Yii::import('application.modules.admin.models.User');

		$criteria=new CDbCriteria;

		

                $criteria->with = array('cliente');// I have added this line


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

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

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

		

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

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

                $criteria->compare('paid',$this->confirmed);

                $criteria->compare('cliente.nome',$this->client_nome, true);

 

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	




		return new CActiveDataProvider(get_class($this), array(


			'criteria'=>$criteria,

    		'sort'=>array(

        		'attributes'=>array(

            	'client_nome'=>array(

                	'asc'=>'cliente.nome',

                	'desc'=>'cliente.nome DESC',

            	),

            	'*',

        		),

    		),			


		));


	}




Add client_nome to safe on search if you want to include in filter search. Hope it works

as I say about use this ( $data instead of $model )


'value'=>'$data->cliente->nome',   

Why i have to declare :

public $client_nome;

if the relation has the attribute?