0

我创建了一个文章和评论模型,并在两者上都有 CRUD。它完美地工作。我现在需要的是在 Comment Crud 中显示 article.title 字段而不是 comment.articleid。我怎样才能做到这一点?

这就是我卡住的地方。我不知道下一步该怎么做或者这是否正确:

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')
    );
}

编辑:

这是我的代码 admin.php 查看文件:

<?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',
        ),
    ),
)); ?>

谢谢。

4

3 回答 3

1

列数组将是这样的:

'columns'=>array(
        'commentid',
        array(
            'name'=>'title',
            'value'=>'$data->article->title',
            'type'=>'text'
        ),
        'content',
        'author',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
于 2011-06-23T15:46:38.480 回答
0

您需要在 Comment 模型中创建此关系,它将通过基于 articleid 加入文章来获取所有匹配的记录

然后,您可以通过替换要显示的值来修改视图。

于 2011-06-23T11:54:36.563 回答
0

你必须使用comment->article->title而不是comment->articleid

于 2011-06-23T13:30:17.657 回答