Skip to content

Commit c941655

Browse files
authored
Merge pull request Insolita#19 from Insolita/17-18-enh
enh Insolita#17 Insolita#18
2 parents 7bc2d6b + 8ed34d8 commit c941655

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@
3838
- Add aftersave callback for create/update actions that called after save model with all relations
3939

4040
1.3.5
41-
- Change UpdateAction, set model scenario before checkAccess calling
41+
- Change UpdateAction, set model scenario before checkAccess calling
42+
43+
1.4.0
44+
- Add callable afterDelete for DeleteAction (#17)
45+
- Add additional param originalModel for afterSave callback for UpdateAction (#18)

src/actions/DeleteAction.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace insolita\fractal\actions;
99

10+
use Closure;
1011
use Yii;
1112
use yii\base\Model;
1213
use yii\web\ForbiddenHttpException;
@@ -26,6 +27,15 @@ class DeleteAction extends JsonApiAction
2627
*/
2728
public $scenario = Model::SCENARIO_DEFAULT;
2829

30+
/**
31+
* @var callable|Closure Callback after save model with all relations
32+
* @example
33+
* 'afterDelete' => function ($model) {
34+
* doSomething();
35+
* }
36+
*/
37+
public $afterDelete = null;
38+
2939
public function init():void
3040
{
3141
parent::init();
@@ -53,7 +63,9 @@ public function run($id):void
5363
if ($model->delete() === false) {
5464
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
5565
}
56-
66+
if ($this->afterDelete !== null) {
67+
call_user_func($this->afterDelete, $model);
68+
}
5769
Yii::$app->getResponse()->setStatusCode(204);
5870
}
5971
}

src/actions/UpdateAction.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class UpdateAction extends JsonApiAction
5858
/**
5959
* @var callable|Closure Callback after save model with all relations
6060
* @example
61-
* 'afterSave' => function ($model) {
61+
* 'afterSave' => function ($model, $originalModel) {
6262
* $model->doSomething();
6363
* }
6464
*/
@@ -92,6 +92,7 @@ public function run($id):Item
9292
if ($this->checkAccess) {
9393
call_user_func($this->checkAccess, $this->id, $model);
9494
}
95+
$originalModel = $model;
9596
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
9697
if (empty($this->getResourceAttributes()) && $this->hasResourceRelationships()) {
9798
$transact = $model::getDb()->beginTransaction();
@@ -124,7 +125,7 @@ public function run($id):Item
124125
}
125126
$model->refresh();
126127
if ($this->afterSave !== null) {
127-
call_user_func($this->afterSave, $model);
128+
call_user_func($this->afterSave, $model, $originalModel);
128129
}
129130
return new Item($model, new $this->transformer, $this->resourceKey);
130131
}

0 commit comments

Comments
 (0)