E
E
EVOSandru62015-12-09 10:19:33
Yii
EVOSandru6, 2015-12-09 10:19:33

How to properly organize search models for CGridView in Yii1?

Good afternoon, I
have a Users model , I want only active users to be displayed in the CGridView , at the same time I want filters by columns to work:

class Users extends ActiveRecord
{
...

public function search()
  {
    $criteria   =   new CDbCriteria;
    $criteria->compare('id'              ,$this->id);
    $criteria->compare('name'            ,$this->name,true);
    $criteria->compare('role_id'         ,$this->role_id);
    $criteria->compare('email'           ,$this->email,true);
    $criteria->compare('password'        ,$this->password,true);
    $criteria->compare('telephone'       ,$this->telephone,true);
    $criteria->compare('sys_date'        ,$this->sys_date);
    $criteria->compare('sys_date_update' ,$this->sys_date_update);
    $criteria->compare('sys_user'        ,$this->sys_user);
                $criteria->compare('exist'           ,$this->exist);
                $criteria->condition = 'exist != 2';

        return new CActiveDataProvider($this,
            [
                'criteria'  =>  $criteria,
                'pagination'=>
                [
                    'pageSize'=>100
                ],
            ]);
  }
...

}

The problem is this:
If I leave it as it is, then the output is carried out correctly - as I need (if exist == 2 , then the user is placed in the basket).
But if I try to filter by any field, I get an error:

PDOException
SQLSTATE[HY093]: Invalid parameter number: :ycp0 (C:\OpenServer\domains\hotel\framework\db\CDbCommand.php:275)
If in the search( ) I comment on the line:
// $criteria->condition = 'exist != 2';
The filter works, but all users leave the basket too.
Please advise how to improve my situation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2015-12-09
@AloneCoder

public function search()
  {
    $criteria   =   new CDbCriteria;
    $criteria->compare('id'              ,$this->id);
    $criteria->compare('name'            ,$this->name,true);
    $criteria->compare('role_id'         ,$this->role_id);
    $criteria->compare('email'           ,$this->email,true);
    $criteria->compare('password'        ,$this->password,true);
    $criteria->compare('telephone'       ,$this->telephone,true);
    $criteria->compare('sys_date'        ,$this->sys_date);
    $criteria->compare('sys_date_update' ,$this->sys_date_update);
    $criteria->compare('sys_user'        ,$this->sys_user);
    $criteria->compare('exist', '<>' . 2);

        return new CActiveDataProvider($this,
            [
                'criteria'  =>  $criteria,
                'pagination'=>
                [
                    'pageSize'=>100
                ],
            ]);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question