Php Yii范围未传递到CListView

Php Yii范围未传递到CListView,php,yii,Php,Yii,我正在尝试将此模型范围传递到我的CListView中 下面是我的评论模型中的范围 public function scopes() { return array( 'lastestComment'=>array( 'alias' => 't', 'select'=>array('t.*,t2.*'), 'join

我正在尝试将此模型范围传递到我的CListView中

下面是我的评论模型中的范围

public function scopes()
    {
        return array(
            'lastestComment'=>array(
                    'alias' => 't',
                    'select'=>array('t.*,t2.*'),
                    'join'=>'JOIN `comments_posts` AS t2',
                    'condition'=>'t.id=t2.commentId',
                    'order'=>'t.createDate ASC',
                    'limit'=>'5'
                )
        );
    }
在我看来,我有这个

$dataProvider=new CActiveDataProvider(Comment::model()->lastestComment());
$this->widget('zii.widgets.CListView', array(
                        'dataProvider'=>$dataProvider,
                        'itemView'=>'_view', //view file location
                    ));

在视图中,然后调用$data,我只能在comments模型中获取值,而不能从scope join中的comments\u post表中获取值。有什么想法吗?

你不必把事情弄得这么复杂,就能实现你想要的

易总是简单的

模型中

在视图中,您可以按如下方式获取注释表中的数据

和comment_post表中的数据


使用关系和范围有什么区别?关系不是像使用defautScope吗?我的理解是,通过这样做。每次我调用注释模型时,它都会查询两个表。即使我只需要1hmmm$data->posts->postId中的数据也不起作用。但是当我打印\u r$数据帖子时,我得到了这个数组[0]=>CommentsPosts对象[\u new:CActiveRecord:private]=>[\u attributes:CActiveRecord:private]=>Array[postId]=>33[make\u code]=>02[model\u code]=>21[year\u Maked]=>2014……不要只是尝试打印,Yii所做的是当你调用这个值时,它执行性能查询,它就是这样做的。所以只要试着打印$data->posts,你就会得到这些值,当我做$data->posts->postd时,我什么也得不到。但是print_r$data->posts'我明白了,你必须把它们放到foreach中,因为它有很多关系。请参阅已编辑的答案
public function relations() {

        return array(
            'posts' => array(self::HAS_MANY, "CommentPosts", array("commentId" => "id")),
        );
    }
$data->column_name
foreach($data->posts as $postings){
   $postings->column_name;
}