Yii 有一个名为Behaviors的特性
,它类似于 php 5.4 的特性,但也适用于早期版本。
软删除行为.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 类