@@ -29,9 +29,10 @@ class CreateAction extends JsonApiAction
29
29
{
30
30
use HasResourceTransformer;
31
31
use HasParentAttributes;
32
+
32
33
/**
33
34
* @var array
34
- * * Configuration for attaching relationships
35
+ * Configuration for attaching relationships
35
36
* Should contains key - relation name and array with
36
37
* idType - php type of resource ids for validation
37
38
* validator = callback for custom id validation
@@ -44,12 +45,24 @@ class CreateAction extends JsonApiAction
44
45
* $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
45
46
* if(count($relatedModels) < $ids) {
46
47
* throw new HttpException(422, 'Invalid photos ids');
47
- * }] ,
48
+ * }} ,
48
49
* ]
49
- **/
50
+ **/
51
+
50
52
public $ allowedRelations = [];
53
+
51
54
/**
52
- * @var string the scenario to be assigned to the new model before it is validated and saved.
55
+ * @var string|callable
56
+ * string - the scenario to be assigned to the model before it is validated and saved.
57
+ * callable - a PHP callable that will be executed during the action.
58
+ * It must return a string representing the scenario to be assigned to the model before it is validated and saved.
59
+ * The signature of the callable should be as follows,
60
+ * ```php
61
+ * function ($action, $model = null) {
62
+ * // $model is the requested model instance.
63
+ * // If null, it means no specific model (e.g. CreateAction)
64
+ * }
65
+ * ```
53
66
*/
54
67
public $ scenario = Model::SCENARIO_DEFAULT ;
55
68
@@ -98,7 +111,7 @@ public function run()
98
111
99
112
/* @var $model \yii\db\ActiveRecord */
100
113
$ model = new $ this ->modelClass ([
101
- 'scenario ' => $ this ->scenario ,
114
+ 'scenario ' => is_callable ( $ this -> scenario ) ? call_user_func ( $ this -> scenario , $ this -> id ) : $ this ->scenario ,
102
115
]);
103
116
RelationshipManager::validateRelationships ($ model , $ this ->getResourceRelationships (), $ this ->allowedRelations );
104
117
$ model ->load ($ this ->getResourceAttributes (), '' );
0 commit comments