| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace app\common\dao\util;
- use think\facade\Db;
- class TableOpsDao
- {
- public function backupAndDeleteBatch(array $ops): void
- {
- foreach ($ops as [$table, $where]) {
- $this->backupAndDelete($table, $where);
- }
- }
- public function backupAndDelete(string $table, array $where): void
- {
- $rows = Db::name($table)->where($where)->select()->toArray();
- if ($rows) {
- Db::name('backup_' . $table)->insertAll($rows);
- Db::name($table)->where($where)->delete();
- }
- }
- public function restore(string $table, array $where): void
- {
- $backupTable = 'backup_' . $table;
- $rows = Db::name($backupTable)->where($where)->select()->toArray();
- if ($rows) {
- foreach ($rows as &$row) {
- if (isset($row['del_time'])) unset($row['del_time']);
- }
- unset($row);
- Db::name($table)->insertAll($rows);
- Db::name($backupTable)->where($where)->delete();
- }
- }
- public function restoreBatch(array $ops): void
- {
- foreach ($ops as [$table, $where]) {
- $this->restore($table, $where);
- }
- }
- }
|