Yii中的范围-在新模型上强制执行模式



对于某些模型,我们在MySQL中使用valid布尔值实现了软删除。

在类中,scopes方法定义如下:

public function scopes() {
    return array(
        'valid'=>array(
            'condition'=>"t.valid=1",
        )
    );
}

这样,当我们加载一个模型时,我们可以调用范围,使其只包括有效的(而不是删除的)模型以及其他查找标准,或者不管它是什么

这不是很枯燥,我想知道是否有另一种方法可以实现同样的事情,也许可以应用于接口,所有模型都源自的抽象Model类,或者,如果使用5.4,则应用于特性。

Yii有一个名为Behaviors的特性这与php5.4特性类似,但也适用于早期版本。

SoftDeleteBehavior.php:

class SoftDeleteBehavior extends CActiveRecordBehavior {
    public $deleteAttribute = 'valid';
    public $deletedValue = 0;
    public function beforeDelete($event) {
        if ($this->deleteAttribute !== null) {
            $this->getOwner()->{$this->deleteAttribute} = $this->deletedValue;
            $this->getOwner()->update(array($this->deleteAttribute));
            // prevent real deletion of record from database
            $event->isValid = false;
        }
    }
    /**
     * Default scope to be applied to active record's default scope.
     * ActiveRecord must call this from our own default scope.
     * @return array the scope to be applied to default scope
     */
    public function defaultScope() {
        return array(
            'condition' => $this->getOwner()->getTableAlias(false,false).'.'.$this->deleteAttribute
                . ' <> '.var_export($this->deletedValue, true),
        );
    }
}

然后我有了这个类来从行为中应用deafultscope:ActiveRecord.php(这个类中当然有更多的方法,缺点是如果需要扩展方法,就需要调用父方法):

class ActiveRecord extends CActiveRecord {
    public function defaultScope() {
        $scope = new CDbCriteria();
        foreach ($this->behaviors() as $name => $value) {
            $behavior = $this->asa($name);
            if ($behavior->enabled && method_exists($behavior,'defaultScope')) {
                $scope->mergeWith($behavior->defaultScope());
            }
        }

        return $scope;
    }
}

然后你在你的模型中使用它:

class MyModel extends ActiveRecord {
    public function behaviors() {
        return array(
            'SoftDeleteBehavior' => array(
                'class' => 'application.components.behaviors.SoftDeleteBehavior',
            ),
        );
    }
}

PROTIP:使用gii 生成模型时,可以指定自己的ActiveRecord类

最新更新