new and empty CActiveDataProvider

Hi,

I’m working on the search function in my website, everything works well but i need to cleanup the code:




    public function actionSearch()

    {

        $dataSet=new CActiveDataProvider('Products',array('criteria'=>array(

        'condition'=>'title=\'impossible_request\'',)));

        $search=Yii::app()->request->getPost('Products');

        if (!empty($search['title'])) {

            $criteria=new CDbCriteria;

            $criteria->compare('title',$search['title'],true);

            $dataSet=Products::getDataProviderByCriteria(50,$criteria);

        }

        $this->render('results',array(

            'dataSet'=>$dataSet,

            )

        ); 

    }



In the first line i initialize $dataSet as an empty CActiveDataProvider (with 0 results) but with a not so clean manner (i’m using a request that i’m sure i’ll get 0 results).

Is there a more clean manner to do that ?

maybe?




    public function actionSearch()

    {

        $search=Yii::app()->request->getPost('Products');

        $criteria=new CDbCriteria;

        if (!empty($search['title'])) {

             $criteria->compare('title',$search['title'],true);

             $pagesize = 50;

        } else {

             $pagesize = 0;

        }

	$dataSet=Products::getDataProviderByCriteria($pagesize,$criteria);

        $this->render('results',array(

            'dataSet'=>$dataSet,

            )

        ); 

    }



this looks like oldschool hardcoded




       $dataSet=new CActiveDataProvider('Products',array('criteria'=>array(

        'condition'=>'title=\'impossible_request\'',)));



Thanks for the reply but i tried it and the CActiveDataProvider returns all the database records.

I also tried :




       } else {

            $criteria->offset=0;

            $criteria->limit=0;

            $pagesize=0;

        }



and it returns all the database record too.

Is Yii able to init an empty CActiveDataProvider or not?

This is one of my favorite Yii tips… just set the data property to an empty array - http://www.yiiframework.com/doc/api/1.1/CDataProvider#data-detail


$dataSet=new CActiveDataProvider('Products',array('data'=>array()));

1 Like

Ok thanks, it worked fine !

Just one more note… following your initial code… it does not make sense to create an object and then override it in the if section…

It’s better to put the creation of the empty CActiveDataProvider on the else statement

I just cleaned up the function :




    public function actionSearch()

    {

        $search=Yii::app()->request->getPost('Products');

        if (!empty($search['title'])) {

            $criteria=new CDbCriteria;

            $criteria->compare('title',$search['title'],true);

            $dataSet=new CActiveDataProvider('Products',array(

            'criteria'=>$criteria,

            'pagination'=>array('pageSize'=>50)

            ));

        } else {

            $dataSet=new CActiveDataProvider('Products');

            $dataSet->setData(array());

        }

        $this->render('results',array(

            'dataSet'=>$dataSet,

            )

        ); 

    }



i just have a quick try in db query with limit 0, and it works there.

@mdomba’s trick is better, thanks for sharing.