title: How to insert more then one entry into database using Kohana ORM?
body: <p>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 <code>project_id</code>, <code>amount</code> and <code>financiers</code> (this is an array) that comes from <code>$_POST</code>.</p>
  
  <p>In controller I have something like this:</p>
  
  <pre><code>$model_for_financiers-&gt;values($_POST);
  
  $model_for_financiers-&gt;save_many();
  </code></pre>
  
  <p>I have created method called <code>save_many()</code> in my ORM model.</p>
  
  <pre><code>public function save_many() {
  
      foreach ($this-&gt;financiers as $financier_id) {
  
          $this-&gt;created_at = time();
          $this-&gt;amount     = $this-&gt;amount * 100;
  
          $this-&gt;financier_id = $financier_id;
  
          $this-&gt;save();
  
          $this-&gt;reset();
  
      }
  
  }
  </code></pre>
  
  <p>...and added <code>financiers</code> as ignored column.</p>
  
  <p>There are two unwanted behaviors (or how should I call them?) in my code:</p>
  
  <ol>
  <li><p>Only the last entry is inserted in the database (it doesn't matter how many IDs are in <code>financiers</code>),</p></li>
  <li><p><code>amount</code> is multiplied as many times as IDs in <code>financiers</code> are. For example, if there are two IDs in that array... it will be multiplied with 100'<strong>00</strong> (last two zeroes are unwanted).</p></li>
  </ol>
  
  <p>Can you help me with this, guys? Thanks in advice.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here comes my second idea. In short, loop in the controller and each time - new instance of the object.</p>
  
  <p>Controller:</p>
  
  <pre><code>foreach ($_POST['financiers'] as $financier_id) {
  
      $model_for_financiers = ORM::factory('Vcn_Project_Financier');
  
      $model_for_financiers-&gt;values(array(
          'amount'       =&gt; $_POST['amount'],
          'project_id'   =&gt; $project_id,
          'financier_id' =&gt; $financier_id
      ));
  
      if ($model_for_financiers-&gt;check()) {
  
          $model_for_financiers-&gt;save();
  
      }
  
  }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function save() {
  
      $this-&gt;created_at = time();
      $this-&gt;amount     = $this-&gt;amount * 100;
  
  //      $this-&gt;reset();
  
      parent::save();
  
  }
  </code></pre>
  
  <p>I really don't know what the f*ck, but, in model's <code>save()</code>, when I try to <code>var_dump()</code> any variable that I have passed to <code>values()</code>, it says <code>NULL</code>. The good news are that it now inserts correct count of entries in the database. The bad news: any data passed to <code>values()</code> are empty.</p>
  
  <p>Help here is really appreciated. :)</p>
  
  <p><strong>Edit #2:</strong></p>
  
  <p>3rd solution that does <strong>not</strong> work. :(</p>
  
  <p>Controller:</p>
  
  <pre><code>$model_for_financiers = ORM::factory('Vcn_Project_Financier');
  
  $model_for_financiers-&gt;save_many($_POST['amount'], $_POST['financiers'], $project_id);
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function save_many($amount, $financiers, $project_id) {
  
      foreach ($financiers as $financier_id) {
  
          $this-&gt;values(array(
              'amount'       =&gt; $amount,
              'financier_id' =&gt; $financier_id,
              'project_id'   =&gt; $project_id
          ));
  
          if ($this-&gt;check()) {
  
              $this-&gt;created_at = time();
              $this-&gt;amount     = $amount * 100;
  
              $this-&gt;save();
  
          }
  
          $this-&gt;reset();
  
      }
  
  }
  </code></pre>
  
  <p>It inserts one only one entry and ignores all that were passed to <code>values()</code> as in solution #2.</p>
  
  <p>What happens?</p>