2011-06-23 76 views
0

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

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

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

感谢。

+0

:哪种模式这段代码来自? –

回答

1

它会是这样的列数组:在你的关系功能

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

该变量不应被引用 –

+0

在这个构造中,它确实需要引用。 – ldg

0

你需要创建在评论模型这种关系,它会通过在条款ArticleID

的基础上加入文章然后你就可以通过更换要显示的值修改意见获取所有匹配的记录。

+0

请填写一些代码。 – emurad

0

你必须使用comment->物品─>标题,而不是comment->条款ArticleID

+0

请填写一些代码。 – emurad