Skip to content

Commit

Permalink
feat: add preSave callback option
Browse files Browse the repository at this point in the history
  • Loading branch information
winternet-studio committed Dec 26, 2024
1 parent dc23a2f commit d8b570a
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/behaviors/LoggingBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class LoggingBehavior extends Behavior {
*/
public $baseClasses = [];

/**
* @var callable : Function to call before saving the log entry. Passed the event and log model. Use `$event->sender` to access the original model.
* Could be used to set additional attributes in the log.
*/
public $preSave = null;

public function events() {
$events = [];
if (!in_array('insert', $this->excludeEvents)) {
Expand Down Expand Up @@ -196,6 +202,10 @@ public function logChanges($event) {

$log->setAttributes($logAttributes);

if (is_callable($this->preSave)) {
call_user_func($this->preSave, $event, $log);
}

if (!$log->save()) {
new \winternet\yii2\UserException('Failed to log the operation.', ['Errors' => $log->getErrors(), 'Model' => $log->toArray() ]);
}
Expand Down

0 comments on commit d8b570a

Please sign in to comment.