php – Yii奇怪的行为:CGridView组用

我有一种奇怪的行为,我将尝试解释.

我正在构建一个报告系统,其中每个用户都可以让其他用户报告他们的个人资料,图片或消息.

在后端,我想显示一个CGridView,其中每个用户连续出现,并报告总时间.

要做到这一点,我正在添加组标准,它运作良好.

但是,如果我想通过任何相关字段过滤视图,我必须添加标准,它适用于过滤或搜索,但结果的总显示不正确显示显示1-2的15结果(s )当它应该显示在报告表中显示2个结果中的1-2时我有15行但只有两个用户,还添加了一个不存在的分页.

车型/ Report.php

public function relations()
{
    return array(
        'reported' => array(self::BELONGS_TO, 'User', 'reported_id'),
    );
}

public function search()
{
    $criteria = new CDbCriteria();
    $criteria->select = array(
        '*',
        'count(*) as reports_count',
    );
    $criteria->group = 't.reported_id';
    $criteria->with = array('reported');

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
        'sort'=>array(
            'attributes'=>array(
                'status_search'=>array(
                    'asc'=>'reported.user_status_id',
                    'desc'=>'reported.user_status_id DESC',
                ),
                'reports_count' => array(
                    'asc' => 'reports_count ASC',
                    'desc' => 'reports_count DESC',
                ),
                '*',
            ),
        ),
        ));
} 

更新:我发现解决方案使用join而不是选择我需要的字段而不是用*选择所有字段:

$criteria->select = array(
        't.id, t.reported_id',
        'count(*) as reports_count',
    );
$criteria->group = 't.reported_id';
$criteria->join = 'LEFT JOIN user u on u.id = t.reported_id';

解决方法:

当使用CActiveDataprovider时,YII不使用条件中建立的’with’来执行’totalItemCount’,因此它将说明没有的结果总数.我建议你手动添加连接条件来克服这个问题:)

$criteria->join = 'left join User u on u.id=t.reported_id'

并删除$criteria->选择并使用CActiveDataProvider的’totalItemCount’方法.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐