I have created a form with the following fields and use json encode before saving to a single database field.
Now I need to populate the fields when using the update form however I am unsure as i can't find any yii2 example.
<?= $form->field( $model, 'seo_information[field1]' )
->textInput( [ 'maxlength' => 255 ] )
->label('Array Field 1) ?>
<?= $form->field( $model, 'seo_information[field2]' )
->textInput( [ 'maxlength' => 255 ] )
->label('Array Field 2') ?>
<?= $form->field( $model, 'seo_information[field3]' )
->textInput( [ 'maxlength' => 255 ] )
->label('Array Field 3') ?>
Controller Create Code
public function actionCreate()
{
$model = new Article();
$model->user_id = Yii::$app->user->id;
if ($model->load(Yii::$app->request->post()))
{
$seo_information = $_POST['Article']['seo_information'];
$model->seo_information = json_encode($seo_information);
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionCreate() { $model = new Article(); $model->user_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post())) { $seo_information = $_POST['Article']['seo_information']; $model->seo_information = json_encode($seo_information); $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } }Could do with some validation.