我可以使用yii CSqlDataProvider执行两个或多个sql语句吗?

时间:2012-01-26 11:08:22

标签: mysql yii yii-components

我试图使用yii获取以下sql语句的结果很少,但是有些错误...... !!!

 $sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";

$count=count(Yii::app()->db->createCommand($sql)->query()); 

$dataProvider=new CSqlDataProvider($sql, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));

3 个答案:

答案 0 :(得分:1)

您正在使用count(Yii::app()->db->createCommand($sql)->query());,我不确定,但我认为query()只返回true或false。尝试使用queryAll(),或者只使用queryRow()

答案 1 :(得分:0)

$sql="SET @pStat=-1; SET @pdt=0;  SELECT |selectPart| FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$selectPart = 'COUNT(1) as cnt';
$count = Yii::app()->db->createCommand(str_replace('|selectPart|', $selectPart, $sql))->queryScalar();
$sql = str_replace('|selectPart|','*',$sql);
$dataProvider=new CSqlDataProvider($sql, array(
        'totalItemCount'=>$count,
        'pagination'=>array(
            'pageSize'=> 10,
        ),
    ));

答案 2 :(得分:0)

尝试使用CArrayDataProvider代替CSqlDataProvider

$sql="SET @pStat=-1; SET @pdt=0;  SELECT * FROM (SELECT !li.gen  AS id,li.logAt, getDuration(gen,logAt) AS duration FROM os_loggerinfo li ) AS a WHERE a.duration>0;";
$result=Yii::app()->db->createCommand($sql)->queryAll(); 
$count=count($result);
$dataProvider=new CArrayDataProvider($result, array(
            'totalItemCount'=>$count,
            'pagination'=>array(
                'pageSize'=> 10,
            ),
        ));