Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6579549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:54:45+00:00 2026-05-25T15:54:45+00:00

I’m using Kohana 3.0 and my task is to insert data in the database.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:54:46+00:00Added an answer on May 25, 2026 at 3:54 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.