Yii CActiveDataProvider项目计数不同

时间:2015-01-15 14:51:40

标签: php yii

我是Yii的新手。我在Yii中有以下代码。

public function actionExport($typeid=0, $locationid=0, $sidx='date_input', $sord='desc',  

$kategori='', $to = 'xls'){
    // inisialisasi criteria query
    $criteria = new CDbCriteria();
    $criteria->order = "$sidx $sord";

    // filter lokasi
    if (is_numeric($locationid) && $locationid !== 0)
    {
        $criteria->with = array('data'=>array(
            'condition'=>'data.locationid=:locationid',
            'params'=>array(':locationid'=>$locationid)
        ));
    } else {
        if (is_numeric($typeid) && $typeid !== 0)
        {
            $criteria->with = array('data.location'=>array(
                'with'=>array(
                    'type'=>array(
                        'condition'=>'type.typeid=:typeid',
                        'params'=>array(':typeid'=>$typeid)
                    )
                )
            ));
        } else {
            $criteria->with = array('data.location'=>array(
                'with'=>array(
                    'type'=>array(
                        'condition'=>'type.type_desc=:type_desc',
                        'params'=>array(':type_desc'=>$kategori)
                    )
                )
            ));
        }
    }

    // filter range tanggal
    if (isset($_REQUEST['startdate'], $_REQUEST['enddate']))
    {
        $startdate = $_REQUEST['startdate'];
        $enddate = $_REQUEST['enddate'];
        $criteria->condition = 'date_input <= :enddate AND date_input >= :startdate';
        $criteria->params = array(':startdate'=>$startdate, ':enddate'=>$enddate);
    }

    $dataProvider = new CActiveDataProvider('InputData', array(
        'criteria'=>$criteria
    ));

    echo $dataProvider->getTotalItemCount(); // this is about 5623

    if($to == 'xls'){
        $this->_toXls($dataProvider->getData());
    }else{

    }
}

此行echo $dataProvider->getTotalItemCount()打印5623,但在循环this->_toXls()中,在excel文件中打印的行只有10行。当我使用print_r($dataProvider->getData())调试数据提供者时,结果相同,这是10项。

我在编写代码时错了吗?

3 个答案:

答案 0 :(得分:1)

您需要禁用分页:

$dataProvider = new CActiveDataProvider('InputData', array(
    'criteria' => $criteria
    'pagination' => false
));

作为补充,您可能需要批量迭代您的项目以节省内存,如this answer

答案 1 :(得分:0)

您应该检查$ dataProvider是否已分页:

 $dataProvider = new CActiveDataProvider('InputData', array(
    'criteria'=>$criteria,
    'pagination' => array(
        'pageSize' => 25,
    ),
));

试试这个并查明行是否为25。

要删除分页,只需将分页设置为false即可。

 $dataProvider = new CActiveDataProvider('InputData', array(
   'criteria'=>$criteria,
   'pagination' => false,
  ),
));

链接:http://www.yiiframework.com/doc/api/1.1/CDataProvider#pagination-detail

答案 2 :(得分:0)

不,你没错。它只是DataProvider的分页。你可以像这样关掉它

$dataProvider = new CActiveDataProvider('InputData', array(
    'criteria'=>$criteria,
    'pagination' => FALSE,
));