Use classic 'SELECT' statement for search

Hi all,

I want to search using the usual SELECT statement for comparing the search parameters. Is it possible to accomplish such task? Can I also do the sorting if ever I accomplished what I need?

Currently, this is what I have in my search():

public function search() {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);

        $criteria->compare('username', $this->username, true);

        $criteria->compare('password', $this->password, true);

        $criteria->compare('email', $this->email, true);

        $criteria->compare('createtime', $this->createtime);

        $criteria->compare('lastvisit', $this->lastvisit);

        $criteria->compare('role', $this->role);

        $criteria->compare('status', $this->status);

        $criteria->compare('salt', $this->salt, true);

        $criteria->compare('superuser', $this->superuser);

        $criteria->compare('activkey', $this->activkey, true);

        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,



Instead of using CActiveDataProvider, you may want to use CArrayDataProvider. Here you can write your own query, and you can also define the sorting.

That’s the stock active record search function, I wouldn’t worry about modifying that function as you’ll probably want to use it in other places. When you get the CActiveDataProvider back you can modify it before passing it into a CListView, or whatever. This will including the sorting, off the top of my head:

$model = new Model( 'search' );

$model->attributes = $_GET['Model'];

$dp = $model->search();

$dp->criteria->order = ' ASC';

$this->widget( 'zii.widgets.CListView', array(

  'dataProvider' => $dp,


) );

You probably meant CSqlDataProvider ?

Hi Mike,

Well, I actually meant CArrayDataProvider cause that’s what I use! I experimented with it and it worked, so I didn’t bother to look for other MUCH SIMPLER and MORE OBVIOUS solution. I really feel silly,hehe…

Thanks a lot!

Don’t feel silly :). I never used CSqlDataProvider myself - i rather sometimes stumble upon interesting components when i study the class reference. In my opinion that’s a nice way to learn more about Yii (and OOP in general): Trying to figure out, what a unkown component could be good for, even if you don’t have any use for it now.

But enough OT …