Skip to content

Commit 9144b9f

Browse files
author
Igor Chepurnoy
committed
Update EditableAction.php
1 parent 34435d4 commit 9144b9f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

EditableAction.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class EditableAction extends Action
3030
* @var bool whether to create a model if a primary key parameter was not found.
3131
*/
3232
public $forceCreate = true;
33+
/**
34+
* @var string default pk column name
35+
*/
36+
public $pkColumn = 'id';
3337

3438
/**
3539
* @inheritdoc
@@ -52,6 +56,11 @@ public function run()
5256
$class = $this->modelClass;
5357
$pk = Yii::$app->request->post('pk');
5458
$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+
}
5564
$value = Yii::$app->request->post('value');
5665
if ($attribute === null) {
5766
throw new BadRequestHttpException("Attribute cannot be empty.");
@@ -60,7 +69,7 @@ public function run()
6069
throw new BadRequestHttpException("Value cannot be empty.");
6170
}
6271
/** @var \Yii\db\ActiveRecord $model */
63-
$model = $class::findOne($pk);
72+
$model = $class::findOne([$this->pkColumn => $pk]);
6473
if (!$model) {
6574
if ($this->forceCreate) { // only useful for models with one editable attribute or no validations
6675
$model = new $class;
@@ -75,7 +84,6 @@ public function run()
7584
if ($this->scenario !== null) {
7685
$model->setScenario($this->scenario);
7786
}
78-
//Collect attributes
7987
$model->$attribute = $value;
8088

8189
if ($model->validate([$attribute])) {

0 commit comments

Comments
 (0)