【发布时间】:2012-06-15 02:51:33
【问题描述】:
我有一个奇怪的行为,我将尝试解释。
我正在构建一个报告系统,每个用户都可以让其他用户报告他们的个人资料、图片或消息。
在后端,我想显示一个CGridView,其中每个用户都出现在一行中,并且报告了总次数。
为此,我将group 添加到标准中,效果很好。
但如果我想按任何相关字段过滤视图,我必须将with 添加到条件中,它适用于过滤或搜索,但结果的总显示不正确显示 正在显示1-2 of 15 result(s) 当它应该显示 Displaying 1-2 of 2 result(s) 如在报告表中我有 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 而不是 with 并选择我需要的字段而不是使用 * 选择所有字段的解决方案:
$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';
【问题讨论】:
-
我遇到了类似的问题,只是反过来。
CGridView显示的结果比寻呼机少,我最终设置了一个非常大的限制。最终这可能是一个Yii错误