在 Datamapper ORM 中更改循环中的对象

发布于 2024-11-06 18:10:46 字数 573 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-11-13 18:10:46

我不确定 $a['stored'] 代表什么,但它不是 Datamapper 中的默认值。

为什么不以相反的方式进行操作,循环遍历帖子键呢?

foreach ($_POST as $key => $val)
{
    $t->$key = $this->input->post($key); 
}
$t->save();

注意:任何不存在的列都将被 Datamapper 忽略。


我实际上为此编写了一个 Datamapper 扩展:

class DM_Data {

    function assign_postdata($object, $fields = NULL)
    {
        // You can pass a different field array if you want
        if ( ! $fields)
        {
            $fields = $object->validation;
        }
        foreach ($fields as $k => $data)
        {
            $rules = isset($data['rules']) ? $data['rules'] : array();

            if ( ! isset($_POST[$k])) continue;

            // Cast value to INT, usually for an empty string.
            if (in_array('integer', $rules))
            {
                $object->$k = (integer) $_POST[$k];
            }
            // Do other manipulation here if desired
            else
            {
                $object->$k = $_POST[$k];
            }

        }
        return $object;
    }

}

您可以使用 $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?

foreach ($_POST as $key => $val)
{
    $t->$key = $this->input->post($key); 
}
$t->save();

Note: Any columns that don't exist will just be ignored by Datamapper.


I actually wrote a Datamapper extension for this:

class DM_Data {

    function assign_postdata($object, $fields = NULL)
    {
        // You can pass a different field array if you want
        if ( ! $fields)
        {
            $fields = $object->validation;
        }
        foreach ($fields as $k => $data)
        {
            $rules = isset($data['rules']) ? $data['rules'] : array();

            if ( ! isset($_POST[$k])) continue;

            // Cast value to INT, usually for an empty string.
            if (in_array('integer', $rules))
            {
                $object->$k = (integer) $_POST[$k];
            }
            // Do other manipulation here if desired
            else
            {
                $object->$k = $_POST[$k];
            }

        }
        return $object;
    }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文