Yii2将表单字段数组保存到单个数据库字段 [英] Yii2 save array of form fields to a single database field

查看:128
本文介绍了Yii2将表单字段数组保存到单个数据库字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Yii2中保存字段数组,当前/默认设置仅适用于非数组字段.

How can I save an array of fields in Yii2 the current/default setup only works for field which aren't array.

以下是我需要保存到单个字段中的表单字段:

Below are the form fields I need to save into the single field:

<div class="repeat">
<table class="wrapper" width="100%">
    <thead>
        <tr>
            <td width="10%" colspan="4"><span class="add">Add</span></td>
        </tr>
    </thead>
    <tbody class="container">
    <tr class="template row">
        <td width="10%"><span class="move">Move</span></td>

        <td width="10%">An Input Field</td>

        <td width="70%">
            <?= $form->field($model, 'field1ofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('Field Label') ?>
            <?= $form->field($model, 'fieldofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('Som field') ?>
            <?= $form->field($model, 'field3ofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('Field Label') ?>
            <?= $form->field($model, 'field4ofarray[{{row-count-placeholder}}]')->dropDownList(['instock' => 'Instock', 'soldout' => 'Sold Out', 'scheduled' => 'Scheduled']); ?>
        </td>

        <td width="10%"><span class="remove">Remove</span></td>
    </tr>
    </tbody>
</table>

当前控制器(我需要知道如何遍历数组并保存以及在表单中保存其他普通字段):

Current Controller (I need to know how I can loop through array and save as well as saving other normal fields in my form):

public function actionCreate()
{
    $model = new GrailWall();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

推荐答案

就我而言,我根本不需要对控制器进行任何更改.

In my case I didn't need to make any changes to the controller at all.

您只需在数据库记录中创建一个字段(如'config_json`),然后在模型中使用getter和setter定义虚拟属性即可.

You can just make a field in your db record, something like 'config_json` and then define a virtual property with getter and setter in your model.

public function getConfig()
{
    return json_decode($this->config_json);
}

public function setConfig($value)
{
    $this->config_json = json_encode($value);
}

还要在规则中将您的虚拟属性设置为安全,以便进行大规模分配.

Also set your virtual property to be safe in the rules so Massive Assignment works.

public function rules()
{
    return [
        [['company_id', 'created_at', 'updated_at'], 'integer'],
        [['class'], 'required'],
        [['config_json'], 'string'],
        [['class'], 'string', 'max' => 255],
        [['config'], 'safe']
    ];
}

现在您可以在视图中设置类似的输入

Now you can set inputs like this in your view

<?= $form->field($model, 'config[ga_id]', ['labelOptions' => ['label' => 'Google Analytics Tracking ID']])->textInput(['maxlength' => true]) ?>

这篇关于Yii2将表单字段数组保存到单个数据库字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆