CGridview and column name


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

	'id'=>'encomenda-linha',

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

	'filter'=>$model,

	'columns'=>array(

               array('name'=>'[color="#FFFF00"]Artista[/color]',

                     'value'=>$model->ritmo->artist),

		array('name'=>'Musica',

                      'value'=>$model->ritmo->title),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

i get this error but i dont know why because ‘Artista’ is the name of the column header and not a property

"Property "EncomendaLinha.Artista" is not defined"

not exactly… just check the documentation:

name (the attribute name of the data model) - http://www.yiiframew…umn#name-detail

header (the header cell text) - http://www.yiiframew…n#header-detail

ok but now i cant display the artist property of the ritmo relation.

i have this:


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

	'id'=>'encomenda-linha',

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

	'filter'=>$model,

	'columns'=>array(

               array('header'=>'Artista',

                     'value'=>'ritmo.artist'),

		array('header'=>'Musica',

                      'value'=>'ritmo.title'),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

I will again refer you to the documentation for "value"… see if it helps you - http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

from first post i can conclude that you use relation ritmo in table and field is artist ( you might declare as public variable in model [color=#1C2837][size=2]EncomendaLinha[/size][/color] )

so value code [size=2]had to work, if you use as above in model.[/size]

[size=2]

[/size]

[size=2][color=#008800]‘value’[/color][color=#666600]=>[/color][color=#000000]$model[/color][color=#666600]->[/color][color=#000000]ritmo[/color][color=#666600]->[/color][color=#000000]artist[/color][/size]

‘value’=>$model->ritmo->artisyes i tried that but i get a new error saying:

"Use of undefined constant General - assumed ‘General’ "

CBaseController->widget("zii.widgets.grid.CGridView", array("id" => "encomenda-linha", "dataProvider" => CActiveDataProvider, "filter" => EncomendaLinha, "columns" => array(array("header" => "Artista", "value" => "General"), array("header" => "Musica", "value" => "Kizomba1"), array("class" => "CButtonColumn"))))

29 ‘value’=>$model->ritmo->title),

30 array(

31 ‘class’=>‘CButtonColumn’,

32 ),

33 ),

34 )); ?>

in above post my mistake, now use as below,


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

yes it works but no filter search input above

I use as below and it working fine for me,


array( 'name'=>'city','header'=>'City', 'value'=>'$data->address->city' ),

I have this:


array('header'=>'Artista',

                     'value'=>'$data->style->artist'

	             'name'=>'artist'),

i have tested the property name with ‘style.artist’,‘artist’,‘data->style->artist’

and always the same result :a blank page

can you post your model code?

The resolution by kiran sharma works well.What i do not understand i wrote in comments:

Encomenda Model


<?php







class EncomendaLinha extends CActiveRecord


{


	public $artist;	//why do you have to declare this two attribute if these are declared on

    public $title;      // relation 'ritmo' 

	/**


	 * Returns the static model of the specified AR class.


	 * @param string $className active record class name.


	 * @return EncomendaLinha the static model class


	 */


	public static function model($className=__CLASS__)


	{


		return parent::model($className);


	}





	/**


	 * @return string the associated database table name


	 */


	public function tableName()


	{


		return 'encomenda_linha';


	}





	/**


	 * @return array validation rules for model attributes.


	 */


	public function rules()


	{


		// NOTE: you should only define rules for those attributes that


		// will receive user inputs.


		return array(


			array('encomenda', 'required'),


			array('style, encomenda', 'numerical', 'integerOnly'=>true),

			

			array('gender, driver_job_style, industries, license, endorsement, artist, title','safe'), // i dont know what safe does and 

																										//why is repeated on search scenario


			// The following rule is used by search().


			// Please remove those attributes that should not be searched.


			array('idencomenda_linha, style, encomenda, artist, title', 'safe', 'on'=>'search'),	


		);


	}





	/**


	 * @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(


			'encomenda' => array(self::BELONGS_TO, 'Encomenda', 'encomenda'),


			'ritmo' => array(self::BELONGS_TO, 'Ritmo', 'style'),


		);


	}





	/**


	 * @return array customized attribute labels (name=>label)


	 */


	public function attributeLabels()


	{


		return array(


			'idencomenda_linha' => 'Idencomenda Linha',


			'style' => Yii::t('app','Style'),


			'encomenda' => 'Encomenda',


		);


	}





	/**


	 * 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.


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


		$criteria=new CDbCriteria;

		$criteria->with = array( 'encomenda','ritmo' );		//yes i understand the join but why encomenda??


               


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


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


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

		

		$criteria->compare('ritmo.artist',$this->artist,true);    //-// added

		$criteria->compare('ritmo.title',$this->title,true);    //-// added





		return new CActiveDataProvider($this, array(


			'criteria'=>$criteria,


		));


	}


        


        


}

View file:


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

        'id'=>'encomenda-linha',

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

        'filter'=>$model,

        'columns'=>array(

               array('name'=>'artist',//this name is the name of the attribute created?

			   		'header'=>'Artist',

                     'value'=>$data->ritmo->artist), // i had to insert '' here to work and why $data??

                array('name'=>'title',

					'header'=>'Title',

                      'value'=>$data->ritmo->title),

                array(

                        'class'=>'CButtonColumn',

                ),

        ),

)); ?>

try this




array(

       'header'=>'Artista',

       'value'=>'$data->style->artist',

       'name'=>'artist',

       'filter'=>Chtml::listData(Style::model()->findAll(),'style_id','artist'),

),




It also works for me however that column sorting is not working,

please advice

Hi Befree,

In order to search and/or sort by an attribute in a related model, you have to do a little trick. Please refer to the following wiki.

http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview

It’s one of the MUST-READs.