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

时间:2011-09-16 15:14:36

标签: orm insert save kohana kohana-orm

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

在控制器中,我有类似的东西:

$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. amountfinanciers中的ID相乘的次数相同。例如,如果该数组中有两个ID ...它将乘以100' 00 (最后两个零是不需要的)。

  3. 你能帮助我吗,伙计们?谢谢你的建议。

    修改

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

    控制器:

    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();
    
    }
    

    我真的不知道f * ck是什么,但是,在模型的save()中,当我尝试var_dump()我传递给values()的任何变量时,它说{ {1}}。好消息是它现在在数据库中插入正确的条目数。坏消息:传递给NULL的任何数据都是空的。

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

    编辑#2:

    工作的第3个解决方案。 :(

    控制器:

    values()

    型号:

    $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(); } } 的条目,如解决方案#2。

    会发生什么?

2 个答案:

答案 0 :(得分:2)

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

所以:

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

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

你应该尝试严格并使用ORM::create()ORM::update()而不是保存(如果你之前做过,你可能会马上调试:)。

编辑:哎呀,抱歉,我忽略了你使用Kohana 3.0的事实,所以没有update()create()分离:)

答案 1 :(得分:0)

我找到了一种如何通过在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;

}

P.S。也许这不是正确的方式,ORM不是为那样的东西设计的,但我需要这样做。去做就对了。 :d