Create a form with associations

筅森魡賤 提交于 2020-01-05 14:03:15

问题


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.

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?


回答1:


The key is to have form data which matches the format of find('all')/saveAll($data)

You need something like the following:

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();

That will generate data in the same format that saveAll expects



来源:https://stackoverflow.com/questions/17734536/create-a-form-with-associations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!