]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Добавление мягкого удаления
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 13 Jan 2025 13:02:17 +0000 (16:02 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 13 Jan 2025 13:02:17 +0000 (16:02 +0300)
erp24/actions/timetable/EditPlanAction.php
erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php [new file with mode: 0644]
erp24/records/Timetable.php
erp24/records/TimetableFact.php
erp24/traits/SoftDeleteTrait.php [new file with mode: 0644]

index a6db96566074c468bebf69adeaf56d5e7eb144bd..b6d556a15dff9dd8c024185c535bc439a19f8b3c 100755 (executable)
@@ -30,7 +30,7 @@ class EditPlanAction extends Action
                     $fact->delete();
                 }
 //                $slot->validate() && $slot->delete();
-                $slot->delete();
+                $slot->softDelete();
             }
             return $this->controller->redirect($this->controller->request->getReferrer() ?: ['timetable/plan']);
         }
diff --git a/erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php b/erp24/migrations/m250113_104214_add_deleted_at_column_to_timetable.php
new file mode 100644 (file)
index 0000000..85870ad
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles adding columns to table `{{%timetable}}`.
+ */
+class m250113_104214_add_deleted_at_column_to_timetable extends Migration
+{
+    const TABLE_NAME = 'erp24.timetable';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME) === null) {
+            return;
+        }
+
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME)->getColumn('deleted_at') === null) {
+            $this->addColumn(self::TABLE_NAME, 'deleted_at', $this->timestamp()->null()->defaultValue(null)->comment('Дата мягкого удаления'));
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME) === null) {
+            return;
+        }
+
+        if ($this->db->schema->getTableSchema(self::TABLE_NAME)->getColumn('deleted_at') !== null) {
+            $this->dropColumn(self::TABLE_NAME, 'deleted_at');
+        }
+    }
+}
index e3ba83c8939da55696e0aa71f7e24784eebcd971..2d4f2709f1f6900af9ee5274c85d376d093a8fe1 100755 (executable)
@@ -6,6 +6,7 @@ namespace yii_app\records;
 use yii\db\ActiveQuery;
 use yii\db\ActiveRecord;
 use yii\helpers\ArrayHelper;
+use yii_app\traits\SoftDeleteTrait;
 
 /**
  * Табель сотрудников
@@ -38,6 +39,8 @@ use yii\helpers\ArrayHelper;
  */
 class Timetable extends ActiveRecord
 {
+    use SoftDeleteTrait;
+
     const STATUS_PENDING = 0;
     const STATUS_VERIFIED = 1;
 
@@ -478,4 +481,18 @@ class Timetable extends ActiveRecord
             'dateAdmins' => $adminsByDate,
         ];
     }
+
+    public static function hasSoftDelete(): bool
+    {
+        return true;
+    }
+
+    public static function find()
+    {
+        $query = parent::find();
+        if (static::hasSoftDelete()) {
+            $query->andWhere(['deleted_at' => null]);
+        }
+        return $query;
+    }
 }
\ No newline at end of file
index 00af40ee3bba15c84115b64bbad5c96071f1be54..bc18839bc473c233da70fd80a71adf5f438d7e9f 100755 (executable)
@@ -38,6 +38,11 @@ class TimetableFact extends Timetable
         );
     }
 
+    public static function hasSoftDelete(): bool
+    {
+        return false;
+    }
+
     public static function find()
     {
         return parent::find()->andWhere(['tabel' => self::TABLE_FACT]);
diff --git a/erp24/traits/SoftDeleteTrait.php b/erp24/traits/SoftDeleteTrait.php
new file mode 100644 (file)
index 0000000..22fed05
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace yii_app\traits;
+
+use yii\db\ActiveQuery;
+
+trait SoftDeleteTrait
+{
+    /**
+     * Удаление записи (soft delete).
+     */
+    public function softDelete()
+    {
+        $this->deleted_at = date('Y-m-d H:i:s');
+        return $this->save(false, ['deleted_at']);
+    }
+
+    /**
+     * Восстановление удаленной записи.
+     */
+    public function restore()
+    {
+        $this->deleted_at = null;
+        return $this->save(false, ['deleted_at']);
+    }
+
+    /**
+     * Проверка, удалена ли запись.
+     */
+    public function isDeleted(): bool
+    {
+        return $this->deleted_at !== null;
+    }
+}
\ No newline at end of file