Category parent name in CGridView

I have a category table which have fields:

id

name

parentId

I want to display category parent name in CGridView.

My widget code:


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

	'id'=>'category-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		'parentId',

		'name',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>

and dataProvider function in Category model


public function search()

	{		

		$criteria=new CDbCriteria;


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

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

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


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

			'criteria'=>$criteria,

		));

	}

What should I change, if I want to display parent caregory name in CGridView, not just parentId?

Change the ‘parentId’ to something like:


array(

'header' => 'Parent',

'type' => 'raw',

'value' => '$data->parent->name'

)

Or create a:


function getParentName()

{

    return $this->parent->name;

}



And use ‘parentName’.

HEllo say_ten,

i have tried your steps but it dint work… :(

table structure:


category_id, category_name, parent_id

can you please shade some light on it…

in cgridview i want to show parent category name instead of parent_id but failed to do so…

Thanks

http://www.yiiframework.com/forum/index.php?/topic/20466-relation-on-the-same-table/

This seems to be the problem you want to solve (the first part of the thread).

So in short, create a relation to the table itself, and you should be able to access the name.

Don’t forget to create a public variable in your model that holds the parentName, like e.g. explained here.

Model:




public $parentName


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(

			'selfParent' => array(self::BELONGS_TO, 'yourtable', 'parentId'),

		);

	}


public function search()

        {               

                $criteria=new CDbCriteria;

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

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

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

                //Add this

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


                return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));

        }



and access it in your gridview like this:




		array(

            'name'=>'',

            'filter' => CHtml::activeTextField($model, 'parentName'), 

            'value' => '$data->selfRelation->parentName',

             ),



Note that I didn’t know your base table name, so you have to fill it in.

On searching and filtering with related models you can find a lot of the forums. I have the feeling you will want to do this next. ;) You might wanna read this blog post:

http://www.mrsoundless.com/post/2011/05/09/Searching-and-sorting-a-column-from-a-related-table-in-a-CGridView.aspx