我有一个表格,在Yii2中有一个名为“city”形式的多选字段.当我提交表格时
帖子数据显示以下内容:
帖子数据显示以下内容:
$_POST['city'] = array('0'=>'City A','1'=>'City B','2'=>'City C')
但我想以序列化形式保存数组,如:
a:3:{i:0;s:6:"City A";i:1;s:6:"City B";i:2;s:6:"City C";}
但我不知道如何在Yii2中保存功能之前修改数据. Followin是我的代码:
if(Yii::$app->request->post()){ $_POST['Adpackage']['Page'] = serialize($_POST['Adpackage']['Page']); $_POST['Adpackage']['fixer_type'] = serialize($_POST['Adpackage']['fixer_type']); } if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view','id' => $model->id]); } else { return $this->render('create',[ 'model' => $model ]); }
请帮我.
public function beforeSave($insert) { if (parent::beforeSave($insert)) { $this->Page = serialize($_POST['Adpackage']['Page']); $this->fixer_type = serialize($_POST['Adpackage']['fixer_type']); return true; } else { return false; } }
只需将此代码放入模型及其工作中即可
这是因为Yii :: $app-> request-> post()在此阶段与$_POST不同.尝试将您的代码更改为:
原文链接:https://www.f2er.com/php/133906.html$post = Yii::$app->request->post(); $post['Adpackage']['Page'] = serialize($post['Adpackage']['Page']); $post['Adpackage']['fixer_type'] = serialize($post['Adpackage']['fixer_type']); $model->load($post);
更新:
最好在ActiveRecord beforeSave()方法上执行此操作.