-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
Hi, I'm trying to use your EditableColumn, but with related model's attribute:
[
'class' => EditableColumn::class,
'attribute' => 'image.alt',
'url' => ['update-related-alt'],
],
Currently your code doesn't allow such use.
I fixed this as follows:
Method run():
public function run()
{
$model = $this->findModelOrCreate();
$attribute = $this->getModelAttribute();
if ($this->preProcess && is_callable($this->preProcess, true)) {
call_user_func($this->preProcess, $model);
}
$model->setScenario($this->scenario);
$value = Yii::$app->request->post('value');
$attributeParts = explode('.', $attribute);
$relatedModel = $model;
foreach ($attributeParts as $attributePart) {
$relatedModel = $relatedModel->$attributePart;
}
$relatedModel = $value;
if ($model->validate([$attribute])) {
return $model->save(false);
} else {
throw new BadRequestHttpException($model->getFirstError($attribute));
}
}
And the method getModelAttribute():
private function getModelAttribute()
{
$attribute = Yii::$app->request->post('name');
/*if (strpos($attribute, '.')) {
$attributeParts = explode('.', $attribute);
$attribute = array_pop($attributeParts);
}*/
if ($attribute === null) {
throw new BadRequestHttpException('Attribute cannot be empty.');
}
return $attribute;
}
Metadata
Metadata
Assignees
Labels
No labels