Commit 9c9654e2 authored by germansokolov13's avatar germansokolov13

Убирает всю привязку к событиям, чтобы полагаться на softDelete(вызывается из…

Убирает всю привязку к событиям, чтобы полагаться на softDelete(вызывается из delete из ActiveRecord). Добавляет ретёртны результатов сейвов. Слегка фиксит forceDelete
parent 72ffb07e
...@@ -36,21 +36,12 @@ class SoftDelete extends Behavior ...@@ -36,21 +36,12 @@ class SoftDelete extends Behavior
*/ */
public $activeValue = 1; public $activeValue = 1;
/**
* @inheritdoc
*/
public function events() {
return [
ActiveRecord::EVENT_BEFORE_DELETE => 'softDelete',
];
}
/** /**
* Set the attribute deleted * Удалить софтделитом. Возращает true/false в зависимости от того, успешно ли
* * @return bool
* @param Event $event
*/ */
public function softDelete($event) { public function softDelete() {
if($this->timeAttribute) { if($this->timeAttribute) {
$attributes[0] = $this->timeAttribute; $attributes[0] = $this->timeAttribute;
$this->owner->$attributes[0] = time(); $this->owner->$attributes[0] = time();
...@@ -60,14 +51,12 @@ class SoftDelete extends Behavior ...@@ -60,14 +51,12 @@ class SoftDelete extends Behavior
$this->owner->$attributes[1] = $this->deletedValue; $this->owner->$attributes[1] = $this->deletedValue;
// save record // save record
$this->owner->save(false, $attributes); return $this->owner->save(false, $attributes);
//prevent real delete
$event->isValid = false;
} }
/** /**
* Restore soft-deleted record * Restore soft-deleted record. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/ */
public function restore() { public function restore() {
if($this->timeAttribute) { if($this->timeAttribute) {
...@@ -79,15 +68,20 @@ class SoftDelete extends Behavior ...@@ -79,15 +68,20 @@ class SoftDelete extends Behavior
$this->owner->$attributes[1] = $this->activeValue; $this->owner->$attributes[1] = $this->activeValue;
// save record // save record
$this->owner->save(false, $attributes); return $this->owner->save(false, $attributes);
} }
/** /**
* Force delete from database * Force delete from database. Возращает true/false в зависимости от того, успешно ли
* @return bool
*/ */
public function forceDelete() { public function forceDelete() {
// store model so that we can detach the behavior and delete as normal // store model so that we can detach the behavior and delete as normal
$model = $this->owner; $model = $this->owner;
$this->detach(); $this->detach();
$model->delete(); $result = $model->delete();
$this->attach($model);
return $result;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment