|
@ -2,7 +2,7 @@ |
|
|
|
|
|
|
|
|
### Configuration |
|
|
### Configuration |
|
|
|
|
|
|
|
|
##### 添加文件: `BitFlagBehavior.php` |
|
|
|
|
|
|
|
|
#### 添加文件: `BitFlagBehavior.php` |
|
|
|
|
|
|
|
|
```php |
|
|
```php |
|
|
<?php |
|
|
<?php |
|
@ -74,11 +74,11 @@ class BitFlagBehavior extends Behavior |
|
|
|
|
|
|
|
|
public function afterFind() |
|
|
public function afterFind() |
|
|
{ |
|
|
{ |
|
|
$owner = $this->owner; |
|
|
|
|
|
|
|
|
valuewner = $this->owner; |
|
|
foreach ($this->bitFlags as $bitFlag => $flags) { |
|
|
foreach ($this->bitFlags as $bitFlag => $flags) { |
|
|
$bitFlagValue = $owner->$bitFlag; |
|
|
|
|
|
|
|
|
$bitFlagValue = valuewner->$bitFlag; |
|
|
foreach ($flags as $pos => $flag) { |
|
|
foreach ($flags as $pos => $flag) { |
|
|
$owner->$flag = ($bitFlagValue & 1 << $pos) >> $pos; |
|
|
|
|
|
|
|
|
valuewner->$flag = ($bitFlagValue & 1 << $pos) >> $pos; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -90,13 +90,13 @@ class BitFlagBehavior extends Behavior |
|
|
*/ |
|
|
*/ |
|
|
public function setBitFlag($bitFlag, $flags) |
|
|
public function setBitFlag($bitFlag, $flags) |
|
|
{ |
|
|
{ |
|
|
$owner = $this->owner; |
|
|
|
|
|
|
|
|
valuewner = $this->owner; |
|
|
foreach ($flags as $pos => $flag) { |
|
|
foreach ($flags as $pos => $flag) { |
|
|
$flagValue = $owner->$flag; |
|
|
|
|
|
|
|
|
$flagValue = valuewner->$flag; |
|
|
if ($flagValue === null) { |
|
|
if ($flagValue === null) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
$owner->$bitFlag = $this->getBitFlagUpdate($owner->$bitFlag, $pos, $flagValue); |
|
|
|
|
|
|
|
|
valuewner->$bitFlag = $this->getBitFlagUpdate(valuewner->$bitFlag, $pos, $flagValue); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -120,38 +120,38 @@ class BitFlagBehavior extends Behavior |
|
|
/** |
|
|
/** |
|
|
* {@inheritdoc} |
|
|
* {@inheritdoc} |
|
|
*/ |
|
|
*/ |
|
|
public function __get($name) |
|
|
|
|
|
|
|
|
public function __get(name) |
|
|
{ |
|
|
{ |
|
|
return $this->_attributes[$name]; |
|
|
|
|
|
|
|
|
return $this->_attributes[name]; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* {@inheritdoc} |
|
|
* {@inheritdoc} |
|
|
*/ |
|
|
*/ |
|
|
public function __set($name, $value) |
|
|
|
|
|
|
|
|
public function __set(name, $value) |
|
|
{ |
|
|
{ |
|
|
$this->_attributes[$name] = $value; |
|
|
|
|
|
|
|
|
$this->_attributes[name] = $value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* {@inheritdoc} |
|
|
* {@inheritdoc} |
|
|
*/ |
|
|
*/ |
|
|
public function canGetProperty($name, $checkVars = true) |
|
|
|
|
|
|
|
|
public function canGetProperty(name, $checkVars = true) |
|
|
{ |
|
|
{ |
|
|
return parent::canGetProperty($name, $checkVars) || ArrayHelper::keyExists($name, $this->_attributes); |
|
|
|
|
|
|
|
|
return parent::canGetProperty(name, $checkVars) || ArrayHelper::keyExists(name, $this->_attributes); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* {@inheritdoc} |
|
|
* {@inheritdoc} |
|
|
*/ |
|
|
*/ |
|
|
public function canSetProperty($name, $checkVars = true) |
|
|
|
|
|
|
|
|
public function canSetProperty(name, $checkVars = true) |
|
|
{ |
|
|
{ |
|
|
return parent::canSetProperty($name, $checkVars) || ArrayHelper::keyExists($name, $this->_attributes); |
|
|
|
|
|
|
|
|
return parent::canSetProperty(name, $checkVars) || ArrayHelper::keyExists(name, $this->_attributes); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
##### 在需要的`Model`里添加`Behavior` |
|
|
|
|
|
|
|
|
#### 在需要的`Model`里添加`Behavior` |
|
|
```php |
|
|
```php |
|
|
<?php |
|
|
<?php |
|
|
|
|
|
|
|
@ -219,7 +219,7 @@ class MyModel extends base\MyModel |
|
|
} |
|
|
} |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
##### 添加文件 `MyModelQuery` |
|
|
|
|
|
|
|
|
#### 添加文件 `MyModelQuery` |
|
|
|
|
|
|
|
|
```php |
|
|
```php |
|
|
<?php |
|
|
<?php |
|
@ -285,11 +285,69 @@ class MyModelQuery extends ActiveQuery |
|
|
|
|
|
|
|
|
### Usage |
|
|
### Usage |
|
|
|
|
|
|
|
|
##### Search |
|
|
|
|
|
|
|
|
#### Search |
|
|
|
|
|
|
|
|
```php |
|
|
```php |
|
|
$myModels = MyModel::find() |
|
|
$myModels = MyModel::find() |
|
|
->andWhereBitFlags(['s0' => 1, 's1' => 0]) |
|
|
->andWhereBitFlags(['s0' => 1, 's1' => 0]) |
|
|
->orWhereBitFlags(['s2' => 0, 's3' => 1]) |
|
|
->orWhereBitFlags(['s2' => 0, 's3' => 1]) |
|
|
->all(); |
|
|
->all(); |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
#### Migration |
|
|
|
|
|
|
|
|
|
|
|
##### 标志位插入到第 `n` 位,默认值为 `k` |
|
|
|
|
|
|
|
|
|
|
|
插入公式: `(value >> n << (n + 1)) + (value & ((1 << n) - 1)) + (k << n)` |
|
|
|
|
|
删除公式: `(value >> (n + 1) << n) + (value & ((1 << n) - 1))` |
|
|
|
|
|
|
|
|
|
|
|
migration: |
|
|
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
use yii\db\Migration; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Class mxxxxxx_xxxxxx_update_{table}_table_{column}_column |
|
|
|
|
|
*/ |
|
|
|
|
|
class mxxxxxx_xxxxxx_update_{table}_table_{column}_column extends Migration |
|
|
|
|
|
{ |
|
|
|
|
|
/** |
|
|
|
|
|
* {@inheritdoc} |
|
|
|
|
|
*/ |
|
|
|
|
|
public function safeUp() |
|
|
|
|
|
{ |
|
|
|
|
|
$this->update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', 1, 'insert', {k})]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* {@inheritdoc} |
|
|
|
|
|
*/ |
|
|
|
|
|
public function safeDown() |
|
|
|
|
|
{ |
|
|
|
|
|
$this->update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', 1, 'delete')]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param string $column |
|
|
|
|
|
* @param int $pos |
|
|
|
|
|
* @param string $operate |
|
|
|
|
|
* @param int $defaultValue |
|
|
|
|
|
* @return \yii\db\Expression |
|
|
|
|
|
*/ |
|
|
|
|
|
public function getBitFlagExpression($column, $pos, $operate, $defaultValue = 0) |
|
|
|
|
|
{ |
|
|
|
|
|
if ($operate == 'insert') { |
|
|
|
|
|
$expresstion = "($column >> $pos << ($pos + 1)) + ($column & ((1 << $pos) - 1)) + ($defaultValue << $pos)"; |
|
|
|
|
|
} elseif ($operate == 'delete') { |
|
|
|
|
|
$expresstion = "($column >> ($pos + 1) << $pos) + ($column & ((1 << $pos) - 1))"; |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new \InvalidArgumentException('$operate: '.$operate); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new \yii\db\Expression($expresstion); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
``` |
|
|
``` |