shulinqian 2012-02-14 11:24:26 2963次浏览 2条回复 0 0 0
public function actionRanking()
{
    $criteria = new CDbCriteria();
    $criteria->group = "bot";
    $criteria->select = "*, COUNT(`id`)  AS `nums`";

    $dataProvider=new CActiveDataProvider('Log', array('criteria' => $criteria));
    $data = $dataProvider->getData(true);
        
    foreach ($data as $value) {
        echo '<pre>';
        print_r($value->id);
        echo '</pre>';
    }
        
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));
}

我要获取nums 应该做些什么?

  • 回复于 2012-02-14 14:33 举报

    我就只想知道

    SELECT *,COUNT(*) AS nums FROM test GROUP BY `cid`;
    

    这样的语句怎么处理,都没有人知道吗?

  • 回复于 2012-02-14 15:21 举报

    结贴:
    终于找到办法了。详细见 http://blog.52xiaoqu.com/31.html
    顺着findAll一路追查下去
    findAll->query->populateRecords->populateRecord 原来是这里做了限制
    文件:framework\db\ar\CActiveRecord.php line:1791

    public function populateRecord($attributes,$callAfterFind=true)
    {
        if($attributes!==false)
        {
            $record=$this->instantiate($attributes);
            $record->setScenario('update');
            $record->init();
            $md=$record->getMetaData();
                
            foreach($attributes as $name=>$value)
            {
                $record->$name=$value;
                if(property_exists($record,$name))  /*这里做了限制*/
                    $record->$name=$value;
                else if(isset($md->columns[$name]))
                    $record->_attributes[$name]=$value;
            }
            $record->_pk=$record->getPrimaryKey();
            $record->attachBehaviors($record->behaviors());
            if($callAfterFind)
                $record->afterFind();
            return $record;
        }
        else
            return null;
    }
    

    知道了是什么限制就好,property_exists是吧?我给模型加上个 var $nums; 运行一下,ok了。真是变态啊!

您需要登录后才可以回复。登录 | 立即注册