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:
-
Only the last entry is inserted in the database (it doesn’t matter how many IDs are in
financiers), -
amountis multiplied as many times as IDs infinanciersare. 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?
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:
And you should try being strict and using
ORM::create()andORM::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()andcreate()separation 🙂