问题描述
如何在 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],[['配置'],'安全']];}
现在您可以在视图中设置这样的输入
= $form->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 将表单字段数组保存到单个数据库字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20
php开发问题
168
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23
php开发问题
287
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23
php开发问题
11
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23
php开发问题
4
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23
php开发问题
17
热门文章
1nohup:忽略输入并将输出附加到“nohup.out"
2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET
3如何将 LDAP 时间戳转换为 Unix 时间戳
4不推荐使用常量 FILTER_SANITIZE_STRING
5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动
6PHP通过phpspreadsheet导入Excel日期数据处理方法
7Analytics API 返回:错误请求 - invalid_grant
8“tlsv1 警报内部错误"握手时
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)