yii2-dynamic-ar
yii2-dynamic-ar copied to clipboard
Failed to save dynamic attributes from POST
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.
Have you verified that dynamic attributes in this model can be set and saved by other methods?
I just created new model and detect that dynamic attributes saved to db, but when i try to access them - I always get null
I'm not in a position to debug this from here. I would need to see all of the relevant code.
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,
]
);
}
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 :)
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!