Yii Framework Forum: new and empty CActiveDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

new and empty CActiveDataProvider how to ?? Rate Topic: -----

#1 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 02 May 2012 - 06:11 AM

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 ?
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 02 May 2012 - 06:41 AM

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\'',)));

I enjoy the Yii.sy coding life here.
0

#3 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 02 May 2012 - 07:46 AM

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?
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 02 May 2012 - 07:47 AM

This is one of my favorite Yii tips... just set the data property to an empty array - http://www.yiiframew...der#data-detail

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


Find more about me.... btw. Do you know your WAN IP?
2

#5 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 02 May 2012 - 07:54 AM

View Postmdomba, on 02 May 2012 - 07:47 AM, said:

This is one of my favorite Yii tips... just set the data property to an empty array - http://www.yiiframew...der#data-detail

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



Ok thanks, it worked fine !
And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 02 May 2012 - 07:57 AM

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
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   saegeek 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 245
  • Joined: 09-December 09
  • Location:Montpellier - France

Posted 02 May 2012 - 07:59 AM

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,
            )
        ); 
    }

And [for] their saying, "Indeed, we have killed the Messiah, Jesus, the son of Mary, the messenger of God ." And they did not kill him, nor did they crucify him; but [another] was made to resemble him to them. And indeed, those who differ over it are in doubt about it. They have no knowledge of it except the following of assumption. And they did not kill him, for certain.Rather, God raised him to Himself.
0

#8 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 02 May 2012 - 08:16 AM

i just have a quick try in db query with limit 0, and it works there.
@mdomba's trick is better, thanks for sharing.
I enjoy the Yii.sy coding life here.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users