File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 3838 - Add aftersave callback for create/update actions that called after save model with all relations
3939
40401.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 )
Original file line number Diff line number Diff line change 77
88namespace insolita \fractal \actions ;
99
10+ use Closure ;
1011use Yii ;
1112use yii \base \Model ;
1213use 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}
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments