简体   繁体   中英

Yii CGridView: how to add a static WHERE condtion?

I've a standard Gii created admin view, which use a CGridView, and it's showing my user table data.

the problem is that user with name 'root' must NOT BE VISIBLE.

Is there a way to add a static where condition " ... and username !='root' " ?

admin.php [view]

'columns'=>array(
    'id',
    'username',
    'password',
    'realname',
    'email',
            .....

user.php [model]

    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('realname',$this->realname,true);
    $criteria->compare('email',$this->email,true);

    ......


    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

您可以像这样使用CDbCriteria的addCondition

 $criteria->addCondition("username !='root'");

Your best option would be to use Yii scopes which are essentially a saved where clause (or other modification of your existing criteria) that you can apply all over your app and only need to change in one place if your criteria ends up changing later.

What makes them even cooler is that you can string them together with other scopes / criteria changes (from users in grids for instance) without having to keep track of what criteria clause is getting changed by what.

A few examples that might apply to your situation. In your controller you probably have something like this:

$users = User::model()->search()->findAll();

Asgaroth's answer answers what you were asking on the surface. But there is so much more you can do (and do easily) using scopes.

If you add the below to your user model:

class User extends CActiveRecord
{
    ......
    public function scopes()
    {
        return array(
            'active'=>array(
                'condition'=>'active=1',
            ),
            'isAdmin'=>array(
                'condition'=>'isAdmin=1',
            ),
        );
    }
}

then you can retrieve active users (with your users' filters still applied) like this in your controller:

$users = User::model()->active()->search()->findAll();

Or you can retrieve all active admin users (without being filtered by your gridview criteria) like this:

$users = User::model()->active()->isAdmin()->findAll();

Default scopes are just an extension of the same idea:

class User extends CActiveRecord
{
    public function defaultScope()
    {
        return array(
            'condition'=>"username != 'root'",
        );
    }
}

If before your isAdmin scope would return the root user, applying the default scope will eliminate the root user from the models returned, as it applies to every User::model() query you make.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM