soft delete user

This commit is contained in:
Kavalar 2018-11-06 11:44:27 +03:00
parent 51952e8c0d
commit 0895403f39
8 changed files with 68 additions and 18 deletions

View File

@ -3,12 +3,14 @@
namespace backend\modules\card\controllers;
use common\classes\Debug;
use common\models\AdditionalFields;
use common\models\CardSkill;
use common\models\FieldsValue;
use Yii;
use backend\modules\card\models\UserCard;
use backend\modules\card\models\UserCardSearch;
use yii\data\ActiveDataProvider;
use yii\db\Expression;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
@ -119,7 +121,12 @@ class UserCardController extends Controller
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
//CardSkill::deleteAll(['card_id' => $id]);
//FieldsValue::deleteAll(['card_id' => $id]);
$model = $this->findModel($id);
$d = new \DateTime();
$model->deleted_at = $d->format('Y-m-d H:i');
$model->save();
return $this->redirect(['index']);
}

View File

@ -54,28 +54,38 @@ class UserCard extends \common\models\UserCard
{
$post = \Yii::$app->request->post('UserCard');
FieldsValue::deleteAll(['card_id' => $this->id]);
foreach ( $post['fields'] as $item) {
$fildsValue = new FieldsValue();
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
$fildsValue->card_id = $this->id;
$fildsValue->save();
if($post['fields']){
FieldsValue::deleteAll(['card_id' => $this->id]);
foreach ( $post['fields'] as $item) {
$fildsValue = new FieldsValue();
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
$fildsValue->card_id = $this->id;
$fildsValue->save();
}
}
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ( $post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
if($post['skill']){
CardSkill::deleteAll(['card_id' => $this->id]);
foreach ( $post['skill'] as $item) {
$skill = new CardSkill();
$skill->skill_id = $item;
$skill->card_id = $this->id;
$skill->save();
}
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}

View File

@ -57,6 +57,8 @@ class UserCardSearch extends UserCard
return $dataProvider;
}
$query->where(['deleted_at' => null]);
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,

View File

@ -53,7 +53,7 @@ $this->params['breadcrumbs'][] = $this->title;
'salary',
[
'attribute' => 'position_id',
'value' => $model->position->name,
'value' => (isset($model->position->name)) ? $model->position->name : 'Без должности',
],
'created_at',
'updated_at',

View File

@ -2,6 +2,7 @@
namespace backend\modules\settings\controllers;
use common\models\UseField;
use Yii;
use backend\modules\settings\models\AdditionalFields;
use backend\modules\settings\models\AdditionalFieldsSearch;
@ -109,6 +110,7 @@ class AdditionalFieldsController extends Controller
*/
public function actionDelete($id)
{
UseField::deleteAll(['field_id' => $id]);
$this->findModel($id)->delete();
return $this->redirect(['index']);

View File

@ -2,6 +2,7 @@
namespace backend\modules\settings\controllers;
use common\models\UseStatus;
use Yii;
use backend\modules\settings\models\Status;
use backend\modules\settings\models\StatusSearch;
@ -110,6 +111,7 @@ class StatusController extends Controller
*/
public function actionDelete($id)
{
UseStatus::deleteAll(['status_id' => $id]);
$this->findModel($id)->delete();
return $this->redirect(['index']);

View File

@ -19,6 +19,7 @@ use yii\db\Expression;
* @property int $status
* @property string $created_at
* @property string $updated_at
* @property string $deleted_at
* @property string $resume
* @property string $salary
* @property int $position_id
@ -59,9 +60,9 @@ class UserCard extends \yii\db\ActiveRecord
public function rules()
{
return [
[['fio', 'status'], 'required'],
[['fio', 'status', 'gender'], 'required'],
[['gender', 'status', 'position_id'], 'integer'],
[['dob', 'created_at', 'updated_at'], 'safe'],
[['dob', 'created_at', 'updated_at', 'deleted_at'], 'safe'],
[['fio', 'passport', 'photo', 'email', 'resume'], 'string', 'max' => 255],
[['salary'], 'string', 'max' => 100],
[['position_id'], 'exist', 'skipOnError' => true, 'targetClass' => Position::class, 'targetAttribute' => ['position_id' => 'id']],
@ -85,6 +86,7 @@ class UserCard extends \yii\db\ActiveRecord
'status' => 'Статус',
'created_at' => 'Дата создания',
'updated_at' => 'Дата редактирование',
'deleted_at' => 'Дата удаления',
'resume' => 'Резюме',
'salary' => 'Зарплата',
'position_id' => 'Должность',

View File

@ -0,0 +1,25 @@
<?php
use yii\db\Migration;
/**
* Handles adding deleted_at to table `user_card`.
*/
class m181106_082016_add_deleted_at_column_to_user_card_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('user_card', 'deleted_at', $this->datetime());
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('user_card', 'deleted_at');
}
}