我正在使用 codeigniter 构建一个应用程序,该应用程序涉及将具有多个电话号码的“看护人”添加到数据库中。从 UI 方面来看,每个电话字段旁边都有一个添加按钮,它使用一些 JavaScript 魔法来克隆自己,以便用户输入另一个号码。提交表单时,电话号码将作为数组提交。
我有两个模型设置:carer和carer_telephone。每个模型都有自己的表格。
我一直在绞尽脑汁想办法让数据映射器在保存之前验证所有关系。例如,目前只显示 carer 字段的验证错误,而 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);
}
对此的任何帮助将不胜感激。甚至只是一个链接到有人在这种情况下工作的示例。
最好的问候,丹