Database relation throwing error

I have created two table




create table images_dir(

id int(10) not null auto_increment,

image_path varchar(55) not null,

image_name varchar(30) not null,

height int(5) not null,

width int(5) not null,

size int(10) not null,

type int(2) not null,

category int(10) not null,

created_on int(10) not null default 0,

hide int(0) not null default 0,

primary key(id),

foreign key(category) references image_categories(id)

)




create table image_categories(

id int(10) not null auto_increment,

name varchar(30) not null,

hide int(0) not null default 0,

primary key(id),

)



And my model class are below with relations




class ImagesDir extends CActiveRecord

{

     public function relations()

     {

		return array(

                    'categoryname' => array(self::BELONGS_TO, 'ImageCategories', 'category'),

		);

     }

}




class ImageCategories extends CActiveRecord

{

     public function relations()

     {

		return array(

                    'CountChildren' => array(self::STAT, 'ImagesDir', 'category'),

                    'categories' => array(self::HAS_MANY, 'ImagesDir', 'category'),

		);

     }

}



In my view i want to show the category of an image, so in my CGridview i am doing the following code




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

	'id'=>'images-dir-grid',

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

	'filter'=>$model,

	'columns'=>array(

		'id',

		'image_name',

		'height',

		'width',

		'size',

                array(

                    'name'=>'category',

                    'value'=>'$data->categoryname'

                ),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



I am getting error with my above code and the same screen shot is attached

Two possible solutions:

  • In CGridView options:



'columns' => array(

	//...

	array(

		'name' => 'category',

		'value' => '$data->categoryname->name',

	),

),



  • In ImageCategories:



public function __toString()

{

	return $this->name;

}