Getting the Article title for the Comment Crud

Hello,

I created an Article and Comment Models and Cruds. Its works perfectly. Why I need now is to have the article.title field displayed in Comment Crud instead of the comment.articleid. How can I do that?

This is where I’m stuck. I don’t know what to do next or if that’s correct:


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(

        'article'=>array(self::BELONGS_TO, 'Article', 'articleid')

    );

}

Thanks.

The relation seems to be OK.

Then you can access article.title from a comment model like this:




$comment=Comment::model()->findByPk($some_id);

$article_title=$comment->ariticle->title;



So, in the columns property of the CGridView in your admin view, instead of:




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

	'id' => 'comment-grid',

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

	'filter' => $model,

	'columns' => array(

		'foo',

		'bar',

		'articleid',

		....

	),

));



you can do like this:




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

	'id' => 'comment-grid',

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

	'filter' => $model,

	'columns' => array(

		'foo',

		'bar',

		array(

			'name' => 'articleid',

			'value' => '$data->article->title',

		),

		....

	),

));



THANK YOU. Worked like a charm but I don’t understand how it worked while ‘$data->article->title’ is enclosed in single quotes which means it will not be parsed! Please clarify.

The statement is eval()'ed at the run time.

CDataColumn::value