Question

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?

Was it helpful?

Solution

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 :)

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top