yii2-dynamic-ar icon indicating copy to clipboard operation
yii2-dynamic-ar copied to clipboard

Failed to save dynamic attributes from POST

Open outOFFspace opened this issue 9 years ago • 6 comments

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.

outOFFspace avatar Oct 18 '16 17:10 outOFFspace

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

tom-- avatar Oct 18 '16 18:10 tom--

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

outOFFspace avatar Oct 18 '16 18:10 outOFFspace

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

tom-- avatar Oct 18 '16 18:10 tom--

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,
            ]
        );
    }

outOFFspace avatar Oct 18 '16 20:10 outOFFspace

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 avatar Oct 22 '16 08:10 dakipro

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!

dakipro avatar Oct 22 '16 09:10 dakipro