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

Failed to save dynamic attributes from POST #16

Open
outOFFspace opened this issue Oct 18, 2016 · 6 comments
Open

Failed to save dynamic attributes from POST #16

outOFFspace opened this issue Oct 18, 2016 · 6 comments

Comments

@outOFFspace
Copy link

outOFFspace commented Oct 18, 2016

If add to from dynamic field e.g:

$form->field($product, 'dynamic_attribute_name')
                ->dropDownList(explode(',', $attribute->variants), ['required' => $attribute->required])
                ->label($attribute->title);

those attributes will not saved.

@tom--
Copy link
Owner

tom-- commented Oct 18, 2016

Have you verified that dynamic attributes in this model can be set and saved by other methods?

@outOFFspace
Copy link
Author

I just created new model and detect that dynamic attributes saved to db, but when i try to access them - I always get null

@tom--
Copy link
Owner

tom-- commented Oct 18, 2016

I'm not in a position to debug this from here. I would need to see all of the relevant code.

@outOFFspace
Copy link
Author

model:

class Product extends \spinitron\dynamicAr\DynamicActiveRecord {

   public static function dynamicColumn()
   {
        return 'params';
    }
}

view _attributes:

if($attributes) {
    foreach($attributes as $attribute) {
        if ($attribute->is_multiple) {
            echo $form->field($product,  $attribute->alias)->dropDownList(explode(',', $attribute->variants));
        } else {
            echo $form->field($product, $attribute->alias);
        }
    }
}

action:

public function actionEdit($id = null)
    {
        $model = null;
        $parent = null;
        if (null === $id) {
            $model = new Product();
            $model->loadDefaultValues();
            $model->currency_id = Currency::getMainCurrency()->id;
        } else {
            $model = Product::findOne($id);
        }

        $attributes = ProductAttribute::find()->all();
        if (null === $model) {
            throw new NotFoundHttpException();
        }

        $post = \Yii::$app->request->post();
        if ($model->load($post)) {
            if ($model->validate()) {
                $saveResult = $model->save();
                if ($saveResult) {
                    $model->invalidateTags();
                    $action = \Yii::$app->request->post('action', 'save');
                    $returnUrl = \Yii::$app->request->get('returnUrl', ['index']);
                    switch ($action) {
                        case 'next':
                            return $this->redirect(
                                [
                                    'edit',
                                    'returnUrl' => $returnUrl
                                ]
                            );
                        case 'back':
                            return $this->redirect($returnUrl);
                        default:
                            return $this->redirect(
                                Url::toRoute([
                                    'edit',
                                    'id' => $model->id,
                                    'returnUrl' => $returnUrl,
                                    'parent_id' => $model->category_id
                                ])
                            );
                    }
                } else {
                    \Yii::$app->session->setFlash('error', \Yii::t('app', 'Cannot save data'));
                }
            } else {
                \Yii::$app->session->setFlash('error', \Yii::t('app', 'Cannot save data'));
            }
        }

        $items = ArrayHelper::map(
            Category::find()->all(),
            'id',
            'name'
        );

        return $this->render(
            'form',
            [
                'model' => $model,
                'attributes' => $attributes,
                'items' => $items,
                'selected' => (array) $model->category_id,
                'parent' => $parent,
            ]
        );
    }

@dakipro
Copy link

dakipro commented Oct 22, 2016

I also get null when I try to access attribute. I tried basic

$model = $this->findModel($id);
$model->specs = 'test';
$model->save();

and I see [BLOB - 19 B] in database, then when I load model and dump it I get
.....
private _attributes -> array (9) [
'id' => integer 2
'dynamic_attribute' => string UTF-8 (19) "?????????specs!test"
]

however
$model->getAttribute('specs') just returns null

I tried different combinations and, but no success.
Using all the latest versions of yii2, php7 and mariadb
Where could I start debugging and fixing?
Any advice would be appreciated :)

@dakipro
Copy link

dakipro commented Oct 22, 2016

I think i have figured it out and it was supper obvious, i was not extending DynamicActiveQuery for my ModelQuery.
Now it appears to be working with simple tests (I will test it further).
Thanks for writing the extension by the way!

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

3 participants