【问题标题】:Yii2 save array of form fields to a single database fieldYii2 将表单字段数组保存到单个数据库字段
【发布时间】:2017-07-01 14:52:15
【问题描述】:

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

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

<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>

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

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

【问题讨论】:

    标签: php arrays yii2


    【解决方案1】:

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

    您可以在您的数据库记录中创建一个字段,例如“config_json”,然后在您的模型中使用 getter 和 setter 定义一个虚拟属性。

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

    还要在规则中将您的虚拟财产设置为安全的,以便大规模分配工作。

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

    现在您可以在视图中设置这样的输入

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

    【讨论】:

      【解决方案2】:

      可以使用loadMultiple(),看this

      这是关于表格/多重输入的。

      否则使用 load($_POST[your_form_name] 加载您需要的模式。

      【讨论】:

        【解决方案3】:

        型号

        public function getNetworkTypeArray()
        {
            return explode(',', $this->network_type);
        }
        
        public function setNetworkTypeArray(array $value)
        {
            $this->network_type = implode(',', $value);
        }
        

        控制器

        public function actionUpdate($id)
        {
            $model = $this->findModel($id);
            if ($model->load(Yii::$app->request->post())) {
                $model->setNetworkTypeArray($model->network_type);
                if ($model->save()) {
                    return $this->redirect(['index']);
                }
            }
        
            $model->network_type = $model->getNetworkTypeArray();
            return $this->render('update', [
                'model' => $model,
            ]);
        }
        

        【讨论】:

          【解决方案4】:
          $form->field($form, 'email[]')->textInput(['id' => 'email-0']);
          

          将呈现:

          <input type="text" name="Form[email][]" id="email-0">
          

          请注意($form, 'email[]') 中的方括号。 ID 属性必须明确设置为唯一,否则会呈现重复的 ID,不符合 W3C/javascript 标准。

          【讨论】:

            【解决方案5】:

            您可以保存在字符串中,而不是保存在数组中,以便在视图中正常使用 tne 变量,例如: fieldofarray 而不是 fieldofarray[]

            并在控制器中使用内爆函数,例如:

              public function actionCreate()
                {
                    $model = new someModel();
            
                    if ($model->load(Yii::$app->request->post())) {
                      $model->fieldofarray = implode (",",$model->fieldofarray);
                      $model->save();
            
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    }
                }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-09-18
              • 1970-01-01
              • 2023-03-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多