@@ -29,9 +29,10 @@ class CreateAction extends JsonApiAction
2929{
3030 use HasResourceTransformer;
3131 use HasParentAttributes;
32+
3233 /**
3334 * @var array
34- * * Configuration for attaching relationships
35+ * Configuration for attaching relationships
3536 * Should contains key - relation name and array with
3637 * idType - php type of resource ids for validation
3738 * validator = callback for custom id validation
@@ -44,12 +45,24 @@ class CreateAction extends JsonApiAction
4445 * $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
4546 * if(count($relatedModels) < $ids) {
4647 * throw new HttpException(422, 'Invalid photos ids');
47- * }] ,
48+ * }} ,
4849 * ]
49- **/
50+ **/
51+
5052 public $ allowedRelations = [];
53+
5154 /**
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+ * ```
5366 */
5467 public $ scenario = Model::SCENARIO_DEFAULT ;
5568
@@ -98,7 +111,7 @@ public function run()
98111
99112 /* @var $model \yii\db\ActiveRecord */
100113 $ model = new $ this ->modelClass ([
101- 'scenario ' => $ this ->scenario ,
114+ 'scenario ' => is_callable ( $ this -> scenario ) ? call_user_func ( $ this -> scenario , $ this -> id ) : $ this ->scenario ,
102115 ]);
103116 RelationshipManager::validateRelationships ($ model , $ this ->getResourceRelationships (), $ this ->allowedRelations );
104117 $ model ->load ($ this ->getResourceAttributes (), '' );
0 commit comments