|
@ -299,6 +299,7 @@ $myModels = MyModel::find() |
|
|
##### 标志位插入到第 `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: |
|
|
migration: |
|
@ -318,7 +319,7 @@ class mxxxxxx_xxxxxx_update_{table}_table_{column}_column extends Migration |
|
|
*/ |
|
|
*/ |
|
|
public function safeUp() |
|
|
public function safeUp() |
|
|
{ |
|
|
{ |
|
|
$this->update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', {pos}, 'insert', {k})]); |
|
|
|
|
|
|
|
|
$this->addBitFlag('{table}', '{column}', {pos}, {defaultValue}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -326,27 +327,30 @@ class mxxxxxx_xxxxxx_update_{table}_table_{column}_column extends Migration |
|
|
*/ |
|
|
*/ |
|
|
public function safeDown() |
|
|
public function safeDown() |
|
|
{ |
|
|
{ |
|
|
$this->update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', {pos}, 'delete')]); |
|
|
|
|
|
|
|
|
$this->deleteBitFlag('{table}', '{column}', {pos}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
|
|
|
* 添加标志位 |
|
|
|
|
|
* @param string $table |
|
|
* @param string $column |
|
|
* @param string $column |
|
|
* @param int $pos |
|
|
* @param int $pos |
|
|
* @param string $operate |
|
|
|
|
|
* @param int $defaultValue |
|
|
* @param int $defaultValue |
|
|
* @return \yii\db\Expression |
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public function getBitFlagExpression($column, $pos, $operate, $defaultValue = 0) |
|
|
|
|
|
|
|
|
public function addBitFlag($table, $column, $pos, $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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
$this->update($table, [$column => new \yii\db\Expression("($column >> $pos << ($pos + 1)) + ($column & ((1 << $pos) - 1)) + ($defaultValue << $pos)")]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return new \yii\db\Expression($expresstion); |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 删除标志位 |
|
|
|
|
|
* @param string $table |
|
|
|
|
|
* @param string $column |
|
|
|
|
|
* @param int $pos |
|
|
|
|
|
*/ |
|
|
|
|
|
public function deleteBitFlag($table, $column, $pos) |
|
|
|
|
|
{ |
|
|
|
|
|
$this->update($table, [$column => new \yii\db\Expression("($column >> ($pos + 1) << $pos) + ($column & ((1 << $pos) - 1))")]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|