Skip to content

Commit

Permalink
Update EditableAction.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Sep 16, 2015
1 parent 34435d4 commit 9144b9f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions EditableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class EditableAction extends Action
* @var bool whether to create a model if a primary key parameter was not found.
*/
public $forceCreate = true;
/**
* @var string default pk column name
*/
public $pkColumn = 'id';

/**
* @inheritdoc
Expand All @@ -52,6 +56,11 @@ public function run()
$class = $this->modelClass;
$pk = Yii::$app->request->post('pk');
$attribute = Yii::$app->request->post('name');
//For attributes with format - relationName.attributeName
if (strpos($attribute, '.')) {
$attributeParts = explode('.', $attribute);
$attribute = array_pop($attributeParts);
}
$value = Yii::$app->request->post('value');
if ($attribute === null) {
throw new BadRequestHttpException("Attribute cannot be empty.");
Expand All @@ -60,7 +69,7 @@ public function run()
throw new BadRequestHttpException("Value cannot be empty.");
}
/** @var \Yii\db\ActiveRecord $model */
$model = $class::findOne($pk);
$model = $class::findOne([$this->pkColumn => $pk]);
if (!$model) {
if ($this->forceCreate) { // only useful for models with one editable attribute or no validations
$model = new $class;
Expand All @@ -75,7 +84,6 @@ public function run()
if ($this->scenario !== null) {
$model->setScenario($this->scenario);
}
//Collect attributes
$model->$attribute = $value;

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

0 comments on commit 9144b9f

Please sign in to comment.