16
16
use yii \base \Model ;
17
17
use yii \db \ActiveRecordInterface ;
18
18
use yii \helpers \Url ;
19
+ use yii \base \InvalidConfigException ;
19
20
use yii \web \ServerErrorHttpException ;
20
21
use function array_keys ;
21
22
use function call_user_func ;
@@ -29,27 +30,40 @@ class CreateAction extends JsonApiAction
29
30
{
30
31
use HasResourceTransformer;
31
32
use HasParentAttributes;
33
+
32
34
/**
33
35
* @var array
34
- * * Configuration for attaching relationships
36
+ * Configuration for attaching relationships
35
37
* Should contains key - relation name and array with
36
38
* idType - php type of resource ids for validation
37
39
* validator = callback for custom id validation
38
40
* Keep it empty for disable this ability
39
41
* @see https://jsonapi.org/format/#crud-creating
40
42
* @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
+
50
54
public $ allowedRelations = [];
55
+
51
56
/**
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
+ * ```
53
67
*/
54
68
public $ scenario = Model::SCENARIO_DEFAULT ;
55
69
@@ -97,9 +111,17 @@ public function run()
97
111
}
98
112
99
113
/* @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
+
103
125
RelationshipManager::validateRelationships ($ model , $ this ->getResourceRelationships (), $ this ->allowedRelations );
104
126
$ model ->load ($ this ->getResourceAttributes (), '' );
105
127
if ($ this ->isParentRestrictionRequired ()) {
0 commit comments