|
|
@ -28,7 +28,6 @@ class BitFlagBehavior extends Behavior |
|
|
|
public function events() |
|
|
|
{ |
|
|
|
return [ |
|
|
|
ActiveRecord::EVENT_INIT => 'eventInit', |
|
|
|
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', |
|
|
|
ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate', |
|
|
|
ActiveRecord::EVENT_AFTER_FIND => 'afterFind', |
|
|
@ -36,24 +35,6 @@ class BitFlagBehavior extends Behavior |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
public function eventInit() |
|
|
|
{ |
|
|
|
$this->addFlagFieldsToOwner(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 添加自定义标志属性 |
|
|
|
*/ |
|
|
|
public function addFlagFieldsToOwner() |
|
|
|
{ |
|
|
|
$owner = $this->owner; |
|
|
|
$fields = []; |
|
|
|
foreach ($owner::BIT_FLAGS as $bigFlag) { |
|
|
|
$fields = ArrayHelper::merge($fields, array_combine($bigFlag, $bigFlag)); |
|
|
|
} |
|
|
|
$owner->_fields = $fields; |
|
|
|
} |
|
|
|
|
|
|
|
public function beforeSave() |
|
|
|
{ |
|
|
|
foreach ($this->bitFlags as $bitFlag => $flags) { |
|
|
@ -159,14 +140,13 @@ class MyModel extends base\MyModel |
|
|
|
// ] |
|
|
|
]; |
|
|
|
|
|
|
|
/** |
|
|
|
* @var string[] 自定义标志字段 |
|
|
|
*/ |
|
|
|
public $_fields; |
|
|
|
|
|
|
|
public function fields() |
|
|
|
{ |
|
|
|
return ArrayHelper::merge(parent::fields(), $this->_fields); |
|
|
|
$fields = []; |
|
|
|
foreach (self::BIT_FLAGS as $bigFlag) { |
|
|
|
$fields = ArrayHelper::merge($fields, array_combine($bigFlag, $bigFlag)); |
|
|
|
} |
|
|
|
return ArrayHelper::merge(parent::fields(), $fields); |
|
|
|
} |
|
|
|
|
|
|
|
public function behaviors() |
|
|
|