title: Saving array to database cakephp
body: <p>I have a form that has multiple entries to the same table, the problem I'm having is that it only sends the last entry to the database and doesn't send the rest. I have used the debug and it only prints out the last field in the form. </p>
  
  <p>With each iteration of <code>field</code> its meant to input into the <code>fields_invoices</code> table a <code>invoice_id, field_id, entered_value</code> </p>
  
  <p>Here is my form</p>
  
  <pre><code>&lt;?php echo $this-&gt;Form-&gt;create('FieldsInvoice'); ?&gt;
      &lt;?php foreach ($fields as $field): ?&gt;
      &lt;?php echo $this-&gt;Form-&gt;hidden('FieldsInvoice.id'); ?&gt;
      &lt;?php echo $this-&gt;Form-&gt;hidden('FieldsInvoice.field_id', array('default' =&gt;$field['Field']['id'])); ?&gt;
      &lt;?php echo $this-&gt;Form-&gt;input('FieldsInvoice.invoice_id', array('default' =&gt;$invoice_id, 'type'=&gt;'hidden')); ?&gt;
      &lt;?php echo $this-&gt;Form-&gt;Input('FieldsInvoice.entered_value',  array('label'=&gt;$field['Field']['name'], 'default' =&gt;$field['Field']['default_value'])); ?&gt;
      &lt;?php endforeach ;?&gt;
      &lt;?php echo $this-&gt;Form-&gt;End('Submit');?&gt;
  </code></pre>
  
  <p>Here is the related controller</p>
  
  <pre><code>    public function create($id)
      {   
      $this-&gt;set('title_for_layout', 'Create Invoice');
      $this-&gt;set('stylesheet_used', 'homestyle');
      $this-&gt;set('image_used', 'eBOXLogoHome.png');   
      $this-&gt;layout='home_layout';
  
  
       if (!is_numeric($id)) throw new BadMethodCallException('I need an ID');
       $this-&gt;Invoice-&gt;id = $id;
       if (!$this-&gt;Invoice-&gt;exists()) throw new NotFoundException('Invalid ID');
  
      $this-&gt;set('invoice_id',$id);
  
      $names = $this-&gt;Invoice-&gt;find('list',array(
      'fields'=&gt;array('template_id'),
      'conditions'=&gt;array('id'=&gt;$id)));
  
      $fields = $this-&gt;Field-&gt;find('all', array(
        'conditions'=&gt;array(
       'template_id'=&gt;$names)));
  
      $this-&gt;set(compact('fields'));
      $this-&gt;set(compact('invoice_id'));
  
      $this-&gt;set('name',$names);
      $this-&gt;Invoice-&gt;create();
      if(empty($this-&gt;data)){
          $this-&gt;data= $this-&gt;Field-&gt;read($id);
      } 
      else{
          if($this-&gt;request-&gt;is('post'))
          {
              //die(debug($this-&gt;data));
  
              $this-&gt;Invoice-&gt;create();
              if($this-&gt;FieldsInvoice-&gt;save($this-&gt;request-&gt;data, array('deep'=&gt;true)));
              {
              $this-&gt;Session-&gt;setFlash('The field has been updated');
              $this-&gt;redirect(array('controller'=&gt;'invoices', 'action'=&gt;'index'));
  
              }
              //else{
              $this-&gt;Session-&gt;setFlash('Could not be saved');
              //}
          }
      }
  }
  
  
  
  
  
  
  
  
  
  \app\Controller\InvoicesController.php (line 134)
  array(
      'FieldsInvoice' =&gt; array(
          (int) 0 =&gt; array(
              'id' =&gt; '',
              'field_id' =&gt; '0',
              'invoice_id' =&gt; '97',
              'entered_value' =&gt; '1'
          ),
          (int) 1 =&gt; array(
              'id' =&gt; '',
              'field_id' =&gt; '99',
              'invoice_id' =&gt; '97',
              'entered_value' =&gt; '2'
          ),
          (int) 2 =&gt; array(
              'id' =&gt; '',
              'field_id' =&gt; '999',
              'invoice_id' =&gt; '97',
              'entered_value' =&gt; '3'
          ),
          (int) 3 =&gt; array(
              'id' =&gt; '',
              'field_id' =&gt; '9999',
              'invoice_id' =&gt; '97',
              'entered_value' =&gt; '4'
          )
      )
  )
  </code></pre>