title: Saving multiple new relations with validation (Codeigniter/DataMapper)
body: <p>I am building an application with codeigniter that involves adding a "carer" with multiple telephone numbers to a database. From the UI side of things, there is an add button next to each telephone field that uses some javascript magic to clone itself in order for the user to input another number. When the form is submitted, the telephone numbers are submitted as an array.</p>
  
  <p>I have two models setup: <strong>carer</strong> and <strong>carer_telephone</strong>. Each model has it's own respective table.</p>
  
  <p>I have been racking my brains for a way that I can get datamapper to validate all the relations before saving them. For example at the moment, only the validation errors for the carer fields are displayed, none for the carer_telephone fields.</p>
  
  <p>Also, I'm not sure if this is the most memory efficient way of dealing with this i.e. creating a new carer_telephone object for every number.</p>
  
  <p>This must be a common setup for many applications but I can't seem to find any documentation on the subject. I am looking for the most standard way of doing this with regards to DataMapper.</p>
  
  <p><strong>The controller so far</strong></p>
  
  <pre><code>function add() {
  
      //Create carer object
      $c = new carer();
  
      //Create carer telephone object
      $t = new carer_telephone();
  
      //Form submitted
      if($this-&gt;input-&gt;post('add_carer')) {
  
          //Set carer data
          $c-&gt;title           = $this-&gt;input-&gt;post('title');
          $c-&gt;first_name      = $this-&gt;input-&gt;post('first_name');
          $c-&gt;family_name = $this-&gt;input-&gt;post('family_name');
          $c-&gt;display_name    = $this-&gt;input-&gt;post('display_name');
          $c-&gt;date_of_birth   = $this-&gt;input-&gt;post('date_of_birth');
          $c-&gt;email_address   = $this-&gt;input-&gt;post('email_address');
          $c-&gt;street_address  = $this-&gt;input-&gt;post('street_address');
          $c-&gt;town            = $this-&gt;input-&gt;post('town');
          $c-&gt;county          = $this-&gt;input-&gt;post('county');
          $c-&gt;postcode        = $this-&gt;input-&gt;post('postcode');
  
          //Set and save telephones
          foreach($this-&gt;input-&gt;post('telephone') as $tel) {
              $t = new carer_telephone();
              $t-&gt;type    = 'test';
              $t-&gt;number  = $tel;
              $c-&gt;save($t);
          }
  
      }
  
      //Store carer object
      $this-&gt;_data['content']['carer'] = $c;
  
      //Load view
      $this-&gt;load-&gt;view('carers/add',$this-&gt;_data);
  
  }
  </code></pre>
  
  <p>Any help on this would be greatly appreciated. Even just a link to an example where somebody has worked on this situation.</p>
  
  <p>Best regards,
  Dan</p>