2011-08-01 89 views
1

我正在用codeigniter构建一个应用程序,其中涉及将具有多个电话号码的“照顾者”添加到数据库。从用户界面方面来看,每个电话区旁边都有一个添加按钮,它使用一些javascript魔术来克隆自己,以便用户输入另一个号码。提交表格时,电话号码将作为数组提交。通过验证保存多个新关系(Codeigniter/DataMapper)

我有两个模型设置:护理人员carer_telephone。每个模型都有自己的表格。

我一直在绞尽脑汁让我可以在保存数据之前验证所有关系。例如,目前仅显示护理人员字段的验证错误,而carer_telephone字段没有显示。

此外,我不确定这是处理这种问题的最有效的内存方式,即为每个数字创建一个新的carer_telephone对象。

这必须是许多应用程序的常见设置,但我似乎无法找到有关该主题的任何文档。我正在寻找与DataMapper相关的最标准方法。

控制器到目前为止

function add() { 

    //Create carer object 
    $c = new carer(); 

    //Create carer telephone object 
    $t = new carer_telephone(); 

    //Form submitted 
    if($this->input->post('add_carer')) { 

     //Set carer data 
     $c->title   = $this->input->post('title'); 
     $c->first_name  = $this->input->post('first_name'); 
     $c->family_name = $this->input->post('family_name'); 
     $c->display_name = $this->input->post('display_name'); 
     $c->date_of_birth = $this->input->post('date_of_birth'); 
     $c->email_address = $this->input->post('email_address'); 
     $c->street_address = $this->input->post('street_address'); 
     $c->town   = $this->input->post('town'); 
     $c->county   = $this->input->post('county'); 
     $c->postcode  = $this->input->post('postcode'); 

     //Set and save telephones 
     foreach($this->input->post('telephone') as $tel) { 
      $t = new carer_telephone(); 
      $t->type = 'test'; 
      $t->number = $tel; 
      $c->save($t); 
     } 

    } 

    //Store carer object 
    $this->_data['content']['carer'] = $c; 

    //Load view 
    $this->load->view('carers/add',$this->_data); 

} 

任何帮助,在此将不胜感激。即使只是一个有人在这种情况下工作的例子。

最好的问候, 丹

回答