1616use yii \base \Model ;
1717use yii \db \ActiveRecordInterface ;
1818use yii \helpers \Url ;
19+ use yii \base \InvalidConfigException ;
1920use yii \web \ServerErrorHttpException ;
2021use function array_keys ;
2122use function call_user_func ;
@@ -29,27 +30,40 @@ class CreateAction extends JsonApiAction
2930{
3031 use HasResourceTransformer;
3132 use HasParentAttributes;
33+
3234 /**
3335 * @var array
34- * * Configuration for attaching relationships
36+ * Configuration for attaching relationships
3537 * Should contains key - relation name and array with
3638 * idType - php type of resource ids for validation
3739 * validator = callback for custom id validation
3840 * Keep it empty for disable this ability
3941 * @see https://jsonapi.org/format/#crud-creating
4042 * @example
41- * 'allowedRelations' => [
42- * 'author' => ['idType' => 'integer'],
43- * 'photos' => ['idType' => 'integer', 'validator' => function($model, array $ids) {
44- * $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
45- * if(count($relatedModels) < $ids) {
46- * throw new HttpException(422, 'Invalid photos ids');
47- * }],
48- * ]
49- **/
43+ * 'allowedRelations' => [
44+ * 'author' => ['idType' => 'integer'],
45+ * 'photos' => ['idType' => 'integer', 'validator' => function($model, array $ids) {
46+ * $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
47+ * if (count($relatedModels) < $ids) {
48+ * throw new HttpException(422, 'Invalid photos ids');
49+ * }
50+ * },
51+ * ]
52+ */
53+
5054 public $ allowedRelations = [];
55+
5156 /**
52- * @var string the scenario to be assigned to the new model before it is validated and saved.
57+ * @var string|callable
58+ * string - the scenario to be assigned to the model before it is validated and saved.
59+ * callable - a PHP callable that will be executed during the action.
60+ * It must return a string representing the scenario to be assigned to the model before it is validated and saved.
61+ * The signature of the callable should be as follows,
62+ * ```php
63+ * function ($action, $model) {
64+ * // $model is the requested model instance.
65+ * }
66+ * ```
5367 */
5468 public $ scenario = Model::SCENARIO_DEFAULT ;
5569
@@ -97,9 +111,17 @@ public function run()
97111 }
98112
99113 /* @var $model \yii\db\ActiveRecord */
100- $ model = new $ this ->modelClass ([
101- 'scenario ' => $ this ->scenario ,
102- ]);
114+ $ model = new $ this ->modelClass ();
115+
116+ if (is_string ($ this ->scenario )) {
117+ $ scenario = $ this ->scenario ;
118+ } elseif (is_callable ($ this ->scenario )) {
119+ $ scenario = call_user_func ($ this ->scenario , $ this ->id , $ model );
120+ } else {
121+ throw new InvalidConfigException ('The "scenario" property must be defined either as a string or as a callable. ' );
122+ }
123+ $ model ->setScenario ($ scenario );
124+
103125 RelationshipManager::validateRelationships ($ model , $ this ->getResourceRelationships (), $ this ->allowedRelations );
104126 $ model ->load ($ this ->getResourceAttributes (), '' );
105127 if ($ this ->isParentRestrictionRequired ()) {
0 commit comments