Skip to content

Commit

Permalink
Добавляет "мягкое" удаление для сделок и других сущностей
Browse files Browse the repository at this point in the history
  • Loading branch information
magniacarpa committed Sep 15, 2020
1 parent 7dde2e2 commit e542928
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"yiisoft/yii2-redis": "^2.0",
"yiisoft/yii2-faker": "^2.0",
"npm-asset/moment": "^2.24",
"wapmorgan/morphos": "^3.2"
"wapmorgan/morphos": "^3.2",
"yii2tech/ar-softdelete": "^1.0"
},
"require-dev": {
"yiisoft/yii2-debug": "~2.1.0",
Expand Down
60 changes: 57 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions console/migrations/m200915_133435_softdelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use yii\db\Migration;

/**
* Class m200915_133435_softdelete
*/
class m200915_133435_softdelete extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('user', 'deleted', $this->boolean()->defaultValue(0));
$this->addColumn('company', 'deleted', $this->boolean()->defaultValue(0));
$this->addColumn('deal', 'deleted', $this->boolean()->defaultValue(0));
$this->addColumn('note', 'deleted', $this->boolean()->defaultValue(0));
$this->addColumn('contact', 'deleted', $this->boolean()->defaultValue(0));
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
echo "m200915_133435_softdelete cannot be reverted.\n";

return false;
}

/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m200915_133435_softdelete cannot be reverted.\n";
return false;
}
*/
}
2 changes: 1 addition & 1 deletion frontend/controllers/DealsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function actionView($id)
$deal = Deal::findOne($id);
$note = new Note();

if (!$deal) {
if (!$deal || $deal->deleted) {
throw new NotFoundHttpException("Сделка с этим ID не найдена");
}

Expand Down
14 changes: 13 additions & 1 deletion frontend/controllers/TableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace frontend\controllers;

use frontend\models\Deal;
use Yii;
use frontend\models\Contact;
use yii\db\ActiveRecord;
Expand Down Expand Up @@ -35,7 +36,7 @@ public function actionCreate()
if ($model->save()) {
Yii::$app->getSession()->setFlash($this->alias . '_create');

return $this->redirect('/' . $this->alias);
return $this->redirect([$this->alias . '/index']);
}
}
}
Expand All @@ -54,4 +55,15 @@ public function actionIndex()

return $this->render('index', ['dataProvider' => $dataProvider, 'model' => $model]);
}

public function actionDelete($id)
{
$deal = Deal::findOne($id);

if ($deal) {
$deal->softDelete();

return $this->redirect([$this->alias . '/index']);
}
}
}
21 changes: 21 additions & 0 deletions frontend/models/Deal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use frontend\interfaces\PersonInterface;
use yii\db\ActiveRecord;
use yii2tech\ar\softdelete\SoftDeleteBehavior;
use yii2tech\ar\softdelete\SoftDeleteQueryBehavior;

/**
* This is the model class for table "deal".
Expand Down Expand Up @@ -64,6 +66,25 @@ public function attributeLabels()
];
}

public function behaviors()
{
return [
'softDeleteBahvior' => [
'class' => SoftDeleteBehavior::class,
'softDeleteAttributeValues' => ['deleted' => true]
]
];
}

public static function find()
{
$query = parent::find();
$query->attachBehavior('softDelete', SoftDeleteQueryBehavior::class);

return $query;
}


public function getFeedItems()
{
$firstItem = new Feed();
Expand Down
2 changes: 1 addition & 1 deletion frontend/models/DealStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function attributeLabels()

public function getDeals()
{
return $this->hasMany(Deal::class, ['status_id' => 'id']);
return $this->hasMany(Deal::class, ['status_id' => 'id'])->notDeleted();
}

public function getDealsAmount()
Expand Down

0 comments on commit e542928

Please sign in to comment.