$fact->delete();
}
// $slot->validate() && $slot->delete();
- $slot->delete();
+ $slot->softDelete();
}
return $this->controller->redirect($this->controller->request->getReferrer() ?: ['timetable/plan']);
}
--- /dev/null
+<?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');
+ }
+ }
+}
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
+use yii_app\traits\SoftDeleteTrait;
/**
* Табель сотрудников
*/
class Timetable extends ActiveRecord
{
+ use SoftDeleteTrait;
+
const STATUS_PENDING = 0;
const STATUS_VERIFIED = 1;
'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
);
}
+ public static function hasSoftDelete(): bool
+ {
+ return false;
+ }
+
public static function find()
{
return parent::find()->andWhere(['tabel' => self::TABLE_FACT]);
--- /dev/null
+<?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