Yii AR:如何选择所选值?

时间:2015-04-10 12:18:40

标签: php activerecord yii

我有SQL查询,它返回唯一值以及此值在DB中的次数。

SELECT sl_keyword, COUNT(sl_keyword)
FROM search_log
GROUP BY sl_keyword

如何在Yii中使用活动记录执行相同的操作?此代码有效,但不返回计数。只有关键字和语言:

$criteria            = new CDbCriteria();
$criteria->select    = 'sl_keyword, sl_language, COUNT(sl_keyword) as times';
$criteria->condition = 'sl_datetime >= :date_from AND sl_datetime <= :date_to';
$criteria->group     = 'sl_keyword, sl_language';
$criteria->offset    = $offset;
$criteria->limit     = $itemsPerPage;
$criteria->params    = array(':date_from' => $dateFrom, ':date_to' => $dateTo);
$result              = self::model()->findAll($criteria);

1 个答案:

答案 0 :(得分:0)

尝试这样(我假设您使用的是foreach循环)

foreach($result as $r)
{
   $times =  $r['times'];  // get like this
   $sl_keyword =  $r['sl_keyword'];
   $sl_language =  $r['sl_language'];
}