创建具有关联的表单 [英] Create a form with associations
问题描述
假设我有一个具有A hasMany B的模型A和B。我想制作一个表格,以允许用户添加模型A的实例,同时还可以添加尽可能多(关联)的模型A的实例。
Suppose I have a models A and B with A hasMany B. I would like to make a form to allow the user to add an instance of the model A, while also being able to add as many (associated) instances of the model B.
但是,做类似 echo $ this-> Form-> input('ABsome_field')$ c的操作$ c>在使用表单帮助器的表单中没有达到预期的效果。正确执行此操作的最佳方法是什么?
However, doing something like echo $this->Form->input('A.B.some_field')
in the form using the form helper does not have the desired effect. What is the best way to do this properly?
推荐答案
关键是要具有与<$ c格式匹配的表单数据$ c>查找('全部') / saveAll($ data)
The key is to have form data which matches the format of find('all')
/saveAll($data)
您需要以下内容:
echo $this->Form->create('A');
echo $this->Form->input('A.id');
echo $this->Form->input('B.0.some_field');
echo $this->Form->input('B.1.some_field');
...
echo $this->Form->submit();
echo $this->Form->end();
生成的数据格式与saveAll期望的格式相同
That will generate data in the same format that saveAll expects
这篇关于创建具有关联的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!