开发者

Getting the Article title for the Comment Crud

开发者 https://www.devze.com 2023-03-14 17:58 出处:网络
I created an Article and Comment Models and have CRUD on both. Its works perfectly. What I need now is to have the article.title field displayed in Comment Crud instead of the comment.articleid. How c

I created an Article and Comment Models and have CRUD on both. Its works perfectly. What 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 rela开发者_StackOverflow中文版tion name and the related
    // class name for the relations automatically generated below.
    return array(
        'article'=>array(self::BELONGS_TO, 'Article', 'articleid')
    );
}

EDIT:

Here's my code admin.php view file:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'comment-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'commentid',
        'articleid',
        'content',
        'author',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

Thanks.


it would be something like this for the columns array:

'columns'=>array(
        'commentid',
        array(
            'name'=>'title',
            'value'=>'$data->article->title',
            'type'=>'text'
        ),
        'content',
        'author',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),


you need to create this relation in Comment model and it will fetch all matched records by joining article on basis of articleid

Then you can modify your views by replacing the values you want to display.


You have to use comment->article->title instead of comment->articleid

0

精彩评论

暂无评论...
验证码 换一张
取 消