|
@ -296,98 +296,11 @@ $myModels = MyModel::find() |
|
|
|
|
|
|
|
|
#### Migration |
|
|
#### Migration |
|
|
|
|
|
|
|
|
##### 从最高位添加标志位,默认值为 `1` (默认值为 `0` 无操作) |
|
|
|
|
|
|
|
|
|
|
|
添加公式: `value + (1 << n)` |
|
|
|
|
|
删除公式: `value - (1 << n)` |
|
|
|
|
|
|
|
|
|
|
|
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}' => new \yii\db\Expression('{column} + (1 << {n})')]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* {@inheritdoc} |
|
|
|
|
|
*/ |
|
|
|
|
|
public function safeDown() |
|
|
|
|
|
{ |
|
|
|
|
|
$this->update('{table}', ['{column}' => new \yii\db\Expression('{column} - (1 << {n})')]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
##### 标志位插入到第 `n` 位,默认值为 `k` |
|
|
##### 标志位插入到第 `n` 位,默认值为 `k` |
|
|
|
|
|
|
|
|
插入公式: `(value >> n << (n + 1)) + (value & ((1 << n) - 1)) + (k << n)` |
|
|
插入公式: `(value >> n << (n + 1)) + (value & ((1 << n) - 1)) + (k << n)` |
|
|
删除公式: `(value >> (n + 1) << n) + (value & ((1 << n) - 1))` |
|
|
删除公式: `(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}' => new \yii\db\Expression('({column} >> {n} << ({n} + 1)) + ({column} & ((1 << {n}) - 1)) + ({k} << {n})')]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* {@inheritdoc} |
|
|
|
|
|
*/ |
|
|
|
|
|
public function safeDown() |
|
|
|
|
|
{ |
|
|
|
|
|
$this->update('{table}', ['{column}' => new \yii\db\Expression('({column} >> ({n} + 1) << {n}) + ({column} & ((1 << {n}) - 1))')]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
封装: |
|
|
|
|
|
|
|
|
|
|
|
```php |
|
|
|
|
|
/** |
|
|
|
|
|
* @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); |
|
|
|
|
|
} |
|
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
|
|
migration: |
|
|
migration: |
|
|
|
|
|
|
|
|
```php |
|
|
```php |
|
|