Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use related model as attribute #12

Open
olegkrivtsov opened this issue Aug 23, 2018 · 1 comment
Open

Can't use related model as attribute #12

olegkrivtsov opened this issue Aug 23, 2018 · 1 comment

Comments

@olegkrivtsov
Copy link

olegkrivtsov commented Aug 23, 2018

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;
    }
@olegkrivtsov
Copy link
Author

Hi, I figured out that this is fixed in dev-master, but not in any stable release. Maybe you add a new release to Composer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant