1. <small id='nX27Y'></small><noframes id='nX27Y'>

    2. <legend id='nX27Y'><style id='nX27Y'><dir id='nX27Y'><q id='nX27Y'></q></dir></style></legend>

        <bdo id='nX27Y'></bdo><ul id='nX27Y'></ul>
      <tfoot id='nX27Y'></tfoot>
      1. <i id='nX27Y'><tr id='nX27Y'><dt id='nX27Y'><q id='nX27Y'><span id='nX27Y'><b id='nX27Y'><form id='nX27Y'><ins id='nX27Y'></ins><ul id='nX27Y'></ul><sub id='nX27Y'></sub></form><legend id='nX27Y'></legend><bdo id='nX27Y'><pre id='nX27Y'><center id='nX27Y'></center></pre></bdo></b><th id='nX27Y'></th></span></q></dt></tr></i><div id='nX27Y'><tfoot id='nX27Y'></tfoot><dl id='nX27Y'><fieldset id='nX27Y'></fieldset></dl></div>

        Yii2 将表单字段数组保存到单个数据库字段

        Yii2 save array of form fields to a single database field(Yii2 将表单字段数组保存到单个数据库字段)
            <tfoot id='O5xYF'></tfoot>
              <tbody id='O5xYF'></tbody>
            <legend id='O5xYF'><style id='O5xYF'><dir id='O5xYF'><q id='O5xYF'></q></dir></style></legend>

            <small id='O5xYF'></small><noframes id='O5xYF'>

                • <bdo id='O5xYF'></bdo><ul id='O5xYF'></ul>

                  <i id='O5xYF'><tr id='O5xYF'><dt id='O5xYF'><q id='O5xYF'><span id='O5xYF'><b id='O5xYF'><form id='O5xYF'><ins id='O5xYF'></ins><ul id='O5xYF'></ul><sub id='O5xYF'></sub></form><legend id='O5xYF'></legend><bdo id='O5xYF'><pre id='O5xYF'><center id='O5xYF'></center></pre></bdo></b><th id='O5xYF'></th></span></q></dt></tr></i><div id='O5xYF'><tfoot id='O5xYF'></tfoot><dl id='O5xYF'><fieldset id='O5xYF'></fieldset></dl></div>
                  本文介绍了Yii2 将表单字段数组保存到单个数据库字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

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

                  <table class="wrapper" width="100%"><头><tr><td width="10%" colspan="4"><span class="add">添加</span></td></tr></thead><tbody class="容器"><tr class="模板行"><td width="10%"><span class="move">Move</span></td><td width="10%">一个输入字段</td><td width="70%"><?= $form->field($model, 'field1ofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('字段标签') ?><?= $form->field($model, 'fieldofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('Som 字段') ?><?= $form->field($model, 'field3ofarray[{{row-count-placeholder}}]')->textInput(['maxlength' => 255])->label('字段标签') ?><?= $form->field($model, 'field4ofarray[{{row-count-placeholder}}]')->dropDownList(['instock' => 'Instock', 'soldout' =>; '售罄', '预定' => '预定']);?></td><td width="10%"><span class="remove">Remove</span></td></tr></tbody>

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

                  公共函数actionCreate(){$model = new GrailWall();if ($model->load(Yii::$app->request->post()) && $model->save()) {返回 $this->redirect(['view', 'id' => $model->id]);} 别的 {返回 $this->render('create', ['模型' =>$模型,]);}}

                  解决方案

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

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

                  公共函数getConfig(){返回 json_decode($this->config_json);}公共函数 setConfig($value){$this->config_json = json_encode($value);}

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

                  公共函数规则(){返回 [[['company_id', 'created_at', 'updated_at'], 'integer'],[['类'],'必需'],[['config_json'], 'string'],[['class'], 'string', 'max' =>255],[['配置'],'安全']];}

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

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

                  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.

                  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 将表单字段数组保存到单个数据库字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Joomla 2.5 much faster than Joomla 1.5 Querywise(Joomla 2.5 比 Joomla 1.5 Querywise 快得多吗)
                  How to share Joomla login session from one joomla website to one ASP.Net MVC website(如何将 Joomla 登录会话从一个 joomla 网站共享到一个 ASP.Net MVC 网站)
                  htaccess redirect root to subdirectory but allow index.php in root AND query strings to function(htaccess 将根重定向到子目录,但允许根和查询字符串中的 index.php 起作用)
                  Joomla include database functions(Joomla 包含数据库功能)
                  nl2br() not working when displaying SQL results(显示 SQL 结果时 nl2br() 不起作用)
                  Joomla 2.5 JFactory::getSession(); seems to be caching in firefox(Joomla 2.5 JFactory::getSession();似乎在 Firefox 中缓存)
                  <tfoot id='HBQz6'></tfoot><legend id='HBQz6'><style id='HBQz6'><dir id='HBQz6'><q id='HBQz6'></q></dir></style></legend>
                    <bdo id='HBQz6'></bdo><ul id='HBQz6'></ul>

                      <tbody id='HBQz6'></tbody>

                      <i id='HBQz6'><tr id='HBQz6'><dt id='HBQz6'><q id='HBQz6'><span id='HBQz6'><b id='HBQz6'><form id='HBQz6'><ins id='HBQz6'></ins><ul id='HBQz6'></ul><sub id='HBQz6'></sub></form><legend id='HBQz6'></legend><bdo id='HBQz6'><pre id='HBQz6'><center id='HBQz6'></center></pre></bdo></b><th id='HBQz6'></th></span></q></dt></tr></i><div id='HBQz6'><tfoot id='HBQz6'></tfoot><dl id='HBQz6'><fieldset id='HBQz6'></fieldset></dl></div>

                        1. <small id='HBQz6'></small><noframes id='HBQz6'>