通过验证保存多个新关系(Codeigniter/DataMapper)
我正在使用 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);
}
对此的任何帮助将不胜感激。即使只是一个有人处理过这种情况的示例的链接。
此致, 担
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.
I have two models setup: carer and carer_telephone. Each model has it's own respective table.
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.
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.
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.
The controller so far
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);
}
Any help on this would be greatly appreciated. Even just a link to an example where somebody has worked on this situation.
Best regards,
Dan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DataMapper 附带了一个数组扩展,可能对您有用: http:// datamapper.wanwizard.eu/pages/extensions/array.html - 允许您将数组数据保存到数据库等。
There is an array extension that comes with DataMapper which might be of use to you: http://datamapper.wanwizard.eu/pages/extensions/array.html - lets you save array data to a database, etc.