创建具有关联的表单 [英] Create a form with associations

查看:59
本文介绍了创建具有关联的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个具有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')在使用表单帮助器的表单中没有达到预期的效果。正确执行此操作的最佳方法是什么?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆