如何使用 Kohana ORM 向数据库中插入多个条目?

发布于 2024-12-05 08:51:36 字数 2845 浏览 0 评论 0原文

我正在使用 Kohana 3.0,我的任务是将数据插入数据库。问题是我需要在其中插入多个条目(所以......在原始 SQL 中 - 多个查询)。我有来自 $_POST 的字段 project_idamountfinanciers (这是一个数组)。

在控制器中,我有这样的内容:

$model_for_financiers->values($_POST);

$model_for_financiers->save_many();

我在 ORM 模型中创建了名为 save_many() 的方法。

public function save_many() {

    foreach ($this->financiers as $financier_id) {

        $this->created_at = time();
        $this->amount     = $this->amount * 100;

        $this->financier_id = $financier_id;

        $this->save();

        $this->reset();

    }

}

...并将 financiers 添加为忽略列。

我的代码中有两个不需要的行为(或者我应该如何称呼它们?):

  1. 仅将最后一个条目插入数据库中(financiers 中有多少个 ID 并不重要) ,

  2. amount 乘以尽可能多的数次作为 ID 金融家是。例如,如果该数组中有两个 ID...它将乘以 100'00(最后两个零是不需要的)。

你们能帮我解决这个问题吗,伙计们?谢谢指教。

编辑:

这是我的第二个想法。简而言之,在控制器中循环并每次 - 对象的新实例。

控制器:

foreach ($_POST['financiers'] as $financier_id) {

    $model_for_financiers = ORM::factory('Vcn_Project_Financier');

    $model_for_financiers->values(array(
        'amount'       => $_POST['amount'],
        'project_id'   => $project_id,
        'financier_id' => $financier_id
    ));

    if ($model_for_financiers->check()) {

        $model_for_financiers->save();

    }

}

模型:

public function save() {

    $this->created_at = time();
    $this->amount     = $this->amount * 100;

//      $this->reset();

    parent::save();

}

我真的不知道他妈的是什么,但是,在模型的 save() 中,当我尝试var_dump() 我拥有的任何变量时传递给 values(),它显示 NULL。好消息是它现在可以在数据库中插入正确的条目数。坏消息:传递给 values() 的任何数据都是空的。

非常感谢这里的帮助。 :)

编辑#2:

第三个解决方案不起作用。 :(

控制器:

$model_for_financiers = ORM::factory('Vcn_Project_Financier');

$model_for_financiers->save_many($_POST['amount'], $_POST['financiers'], $project_id);

模型:

public function save_many($amount, $financiers, $project_id) {

    foreach ($financiers as $financier_id) {

        $this->values(array(
            'amount'       => $amount,
            'financier_id' => $financier_id,
            'project_id'   => $project_id
        ));

        if ($this->check()) {

            $this->created_at = time();
            $this->amount     = $amount * 100;

            $this->save();

        }

        $this->reset();

    }

}

它只插入一个条目,并忽略所有传递给 values() 的条目,如解决方案 #2 中所示。

会发生什么?

I'm using Kohana 3.0 and my task is to insert data in the database. Problem is that I need to insert more then one entry in it (so... in raw SQL - more than one query). I have fields project_id, amount and financiers (this is an array) that comes from $_POST.

In controller I have something like this:

$model_for_financiers->values($_POST);

$model_for_financiers->save_many();

I have created method called save_many() in my ORM model.

public function save_many() {

    foreach ($this->financiers as $financier_id) {

        $this->created_at = time();
        $this->amount     = $this->amount * 100;

        $this->financier_id = $financier_id;

        $this->save();

        $this->reset();

    }

}

...and added financiers as ignored column.

There are two unwanted behaviors (or how should I call them?) in my code:

  1. Only the last entry is inserted in the database (it doesn't matter how many IDs are in financiers),

  2. amount is multiplied as many times as IDs in financiers are. For example, if there are two IDs in that array... it will be multiplied with 100'00 (last two zeroes are unwanted).

Can you help me with this, guys? Thanks in advice.

Edit:

Here comes my second idea. In short, loop in the controller and each time - new instance of the object.

Controller:

foreach ($_POST['financiers'] as $financier_id) {

    $model_for_financiers = ORM::factory('Vcn_Project_Financier');

    $model_for_financiers->values(array(
        'amount'       => $_POST['amount'],
        'project_id'   => $project_id,
        'financier_id' => $financier_id
    ));

    if ($model_for_financiers->check()) {

        $model_for_financiers->save();

    }

}

Model:

public function save() {

    $this->created_at = time();
    $this->amount     = $this->amount * 100;

//      $this->reset();

    parent::save();

}

I really don't know what the f*ck, but, in model's save(), when I try to var_dump() any variable that I have passed to values(), it says NULL. The good news are that it now inserts correct count of entries in the database. The bad news: any data passed to values() are empty.

Help here is really appreciated. :)

Edit #2:

3rd solution that does not work. :(

Controller:

$model_for_financiers = ORM::factory('Vcn_Project_Financier');

$model_for_financiers->save_many($_POST['amount'], $_POST['financiers'], $project_id);

Model:

public function save_many($amount, $financiers, $project_id) {

    foreach ($financiers as $financier_id) {

        $this->values(array(
            'amount'       => $amount,
            'financier_id' => $financier_id,
            'project_id'   => $project_id
        ));

        if ($this->check()) {

            $this->created_at = time();
            $this->amount     = $amount * 100;

            $this->save();

        }

        $this->reset();

    }

}

It inserts one only one entry and ignores all that were passed to values() as in solution #2.

What happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷情 2024-12-12 08:51:36

模型仅代表一行数据,整个 ORM 都是围绕该概念构建的。如果你想保存多个结果,你必须在你使用的方法中实例化每个新对象,而不是使用 $this (它只会创建一次行并在每个其他迭代中更新它,因为你正在使用 save())。

所以:

foreach ($whatevers as $data)
{
    $row = new Model_Whatever;

    $row->values($data)->create();
}

你应该尝试严格地使用 ORM::create()ORM::update() 而不是保存(你可能会立即调试它,如果你以前这样做过:))。

编辑:哎呀,抱歉,我忽略了您正在使用 Kohana 3.0 的事实,因此没有 update()create() 分离:)

A model represents only one row of data and the whole ORM is built around that concept. If you want to save multiple results, you'll have to instantiate each new object inside of the method you're using for it, not use $this (it'll only create the row once and update it on every other iteration because you're using save()).

So:

foreach ($whatevers as $data)
{
    $row = new Model_Whatever;

    $row->values($data)->create();
}

And you should try being strict and using ORM::create() and ORM::update()instead of save (you'd probably debug it right away if you did that before :) ).

Edit: oops, sorry I overlooked the fact you're using Kohana 3.0, so no update() and create() separation :)

娜些时光,永不杰束 2024-12-12 08:51:36

我找到了一种方法,只需调用 ORM 类中的一个方法即可添加多个条目。控制器中没有循环或其他...:)

/**
 * Tries to save multiple entries into the database. If there are problems
 * with validation, method won't save current entry in the database (entries
 * before will be saved) and will stop itself by returning `false`.
 *
 * @param  array   Data (for example, `$_POST`)
 * @return boolean Success?
 */
function try_to_save_many($data) {

    foreach ($data['people'] as $person_id) {

        $this->clear();

        $this->member_id  = $person_id;
        $this->project_id = $data['project_id'];

        if ($this->check()) {

            $this->save();

        } else {

            return false;

        }

    }

    return true;

}

PS 也许这不是正确的方法,ORM 不是为类似的东西而设计的,但我需要这样做。去做就对了。 :D

I found a way how to add multiple entries just by calling one method in ORM class. No loops in controller or whatever... :)

/**
 * Tries to save multiple entries into the database. If there are problems
 * with validation, method won't save current entry in the database (entries
 * before will be saved) and will stop itself by returning `false`.
 *
 * @param  array   Data (for example, `$_POST`)
 * @return boolean Success?
 */
function try_to_save_many($data) {

    foreach ($data['people'] as $person_id) {

        $this->clear();

        $this->member_id  = $person_id;
        $this->project_id = $data['project_id'];

        if ($this->check()) {

            $this->save();

        } else {

            return false;

        }

    }

    return true;

}

P.S. Maybe it's not the correct way and ORM is not designed for stuff like that, but I needed to do it. Just do it. :D

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