From e9b6e3770b90382b41c5bee2bb181b4451de17a6 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Thu, 6 Jul 2023 18:20:06 +0300 Subject: [PATCH] add `SoftDeleteBehavior::$invokeDeleteEvents` --- src/SoftDeleteBehavior.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/SoftDeleteBehavior.php b/src/SoftDeleteBehavior.php index a01b61c..9dca519 100644 --- a/src/SoftDeleteBehavior.php +++ b/src/SoftDeleteBehavior.php @@ -87,6 +87,11 @@ class SoftDeleteBehavior extends CBehavior * in format: `[attributeName => attributeValue]`. If not set value will be automatically detected from {@see softDeleteAttributeValues}. */ public $restoreAttributeValues; + /** + * @var bool whether to invoke owner {@see \CActiveRecord::beforeDelete()} and {@see \CActiveRecord::afterDelete()} + * while performing soft delete. This option affects only {@see softDelete()} method. + */ + public $invokeDeleteEvents = true; /** * @var callable|null callback, which execution determines if record should be "hard" deleted instead of being marked * as deleted. Callback should match following signature: `bool function(\CActiveRecord $model)` @@ -180,7 +185,17 @@ public function softDelete(): bool return $this->owner->delete(); } - return $this->softDeleteInternal(); + if ($this->invokeDeleteEvents && !$this->owner->evaluateExpression('$this->beforeDelete()')) { + return false; + } + + $result = $this->softDeleteInternal(); + + if ($this->invokeDeleteEvents) { + $this->owner->evaluateExpression('$this->afterDelete()'); + } + + return $result; } /**