Php Yii2使用存储在数组中的值填充更新表单
我创建了一个包含以下字段的表单,并在保存到单个数据库字段之前使用json编码 现在我需要在使用更新表单时填充字段,但是我不确定,因为我找不到任何yii2示例Php Yii2使用存储在数组中的值填充更新表单,php,activerecord,model-view-controller,yii2,Php,Activerecord,Model View Controller,Yii2,我创建了一个包含以下字段的表单,并在保存到单个数据库字段之前使用json编码 现在我需要在使用更新表单时填充字段,但是我不确定,因为我找不到任何yii2示例 <?= $form->field( $model, 'seo_information[field1]' ) ->textInput( [ 'maxlength' => 255 ] ) ->label('Array Field 1) ?> <?= $form-
<?= $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') ?>
这应该做到:
else
{
$model->seo_information = json_decode($model->seo_information, true);
return $this->render('create', [
'model' => $model,
]);
}
如果数据库中的值为空,也可以这样做。您能展示一下如何进行json_编码吗?它是在控制器还是模型中?
public function actionCreate(){$model=new Article();$model->user\u id=Yii::$app->user->id;if($model->load(Yii::$app->request->post()){$seo\u information=$\u post['Article']['seo\u information'];$model->seo\u information=json\u encode($seo_信息);$model->save();返回$this->redirect(['view','id'=>$model->id]);}否则{返回$this->render('create',['model'=>$model,]);}
可能需要一些验证。哦……请将其放入您的问题中!请编辑问题并插入代码。
else
{
$model->seo_information = json_decode($model->seo_information, true);
return $this->render('create', [
'model' => $model,
]);
}