在 Datamapper ORM 中更改循环中的对象
我正在尝试在 Codeigniter 的 Datamapper 中保存长表单。如果我传递这样的值,我就可以保存表单
$t->brandName = $this->input->post('brandName');
$t->specialNotes = $this->input->post('specialNotes');
$t->name = $this->input->post('name');
现在,如果我调用 save 方法,它会起作用
$t->save();
因为表单很大,所以我尝试在 foreach 中添加对象值,
$a = get_object_vars($t);
foreach ($a['stored'] as $k => $val){
$t->$k = $this->input->post("$k");
}
但是如果我调用 $t->save ()
它不起作用。
I'm trying to save a long form in Codeigniter's Datamapper. I'm able to save the form if I pass the value like this
$t->brandName = $this->input->post('brandName');
$t->specialNotes = $this->input->post('specialNotes');
$t->name = $this->input->post('name');
Now if I call save method it works
$t->save();
Since the form is big I tried to add object values in foreach
$a = get_object_vars($t);
foreach ($a['stored'] as $k => $val){
$t->$k = $this->input->post("$k");
}
however if I call the $t->save()
it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定
$a['stored']
代表什么,但它不是 Datamapper 中的默认值。为什么不以相反的方式进行操作,循环遍历帖子键呢?
注意:任何不存在的列都将被 Datamapper 忽略。
我实际上为此编写了一个 Datamapper 扩展:
您可以使用
$t->assign_postdata()->save()
,并且可以选择传递一个字段数组来更新到函数(在 datamapper 中)验证格式)。然而,我忘记了为什么使用它......但我删除了一些自定义的东西。如果您经常这样做,这对您应该很有用。这绝对节省了我的时间。I'm not sure what
$a['stored']
represents, but it's nothing that's default in Datamapper.Why don't you do it the opposite way, looping through the post keys?
Note: Any columns that don't exist will just be ignored by Datamapper.
I actually wrote a Datamapper extension for this:
You can use
$t->assign_postdata()->save()
, and optionally pass an array of fields to update to the function (in the datamapper validation format). However, I forget why I use that... but I removed some of the custom stuff. This should be useful for you if you are doing this a lot. It definitely saves me time.