@@ -30,6 +30,10 @@ class EditableAction extends Action
30
30
* @var bool whether to create a model if a primary key parameter was not found.
31
31
*/
32
32
public $ forceCreate = true ;
33
+ /**
34
+ * @var string default pk column name
35
+ */
36
+ public $ pkColumn = 'id ' ;
33
37
34
38
/**
35
39
* @inheritdoc
@@ -52,6 +56,11 @@ public function run()
52
56
$ class = $ this ->modelClass ;
53
57
$ pk = Yii::$ app ->request ->post ('pk ' );
54
58
$ attribute = Yii::$ app ->request ->post ('name ' );
59
+ //For attributes with format - relationName.attributeName
60
+ if (strpos ($ attribute , '. ' )) {
61
+ $ attributeParts = explode ('. ' , $ attribute );
62
+ $ attribute = array_pop ($ attributeParts );
63
+ }
55
64
$ value = Yii::$ app ->request ->post ('value ' );
56
65
if ($ attribute === null ) {
57
66
throw new BadRequestHttpException ("Attribute cannot be empty. " );
@@ -60,7 +69,7 @@ public function run()
60
69
throw new BadRequestHttpException ("Value cannot be empty. " );
61
70
}
62
71
/** @var \Yii\db\ActiveRecord $model */
63
- $ model = $ class ::findOne ($ pk );
72
+ $ model = $ class ::findOne ([ $ this -> pkColumn => $ pk ] );
64
73
if (!$ model ) {
65
74
if ($ this ->forceCreate ) { // only useful for models with one editable attribute or no validations
66
75
$ model = new $ class ;
@@ -75,7 +84,6 @@ public function run()
75
84
if ($ this ->scenario !== null ) {
76
85
$ model ->setScenario ($ this ->scenario );
77
86
}
78
- //Collect attributes
79
87
$ model ->$ attribute = $ value ;
80
88
81
89
if ($ model ->validate ([$ attribute ])) {
0 commit comments