问题
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