2012-01-26 89 views
1

我想獲得使用yii後面的sql語句的幾個結果,但有一些錯誤.... !!!我可以使用yii CSqlDataProvider執行兩個或多個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, 
      ), 
     )); 

回答

1

您使用count(Yii::app()->db->createCommand($sql)->query());,我不知道,但我認爲query()剛剛返回true或false。嘗試使用queryAll(),或者如果它只是一個單行使用queryRow()

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, 
     ), 
    )); 
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, 
      ), 
     )); 
相關問題