Cakephp:关于带有多选的 saveall() 的问题
我想知道最简洁的方法是实现 cakephp 表单,其中 1 个控件是多选,其余是文本字段或单选,然后使用 saveall() 将数据作为多行插入。例如,使用以下值选择表单:
textfield A value=Foo
多选 B 值=美国、墨西哥、加拿大
单=选择 C value=10
所以我想使用 saveall() 将这些行插入数据库: 符,美国,10 富,墨西哥,10 Foo,Canada,10
现在我知道在添加视图中我可以使用这种格式作为输入语句:
input('Model.0.field1',...)
但我想知道是否可以以相同的形式混合使用输入格式如下 输入('模型.field2',....).
更新: 当我混合和匹配单选和多选控件时,表单数据会像这样提交:
Array
(
[Alert] => Array
(
[schedule_id] => 75
[user_id] => 6
[0] => Array
(
[frequency] => Array
(
[0] => WEEKLY
[1] => MONTHLY
)
)
[limit_value] => .03
[limit_adjustment] => 0
[type] => LIMIT
[disabled] => 0
)
)
我尝试将该数据传递到 saveall() 中,但它将它视为单个记录。
Update2:我认为 saveAll() 要求多行数据的格式如下:
Array
(
[Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
所以看起来在提交之后我将需要一些 javascript 代码来重组数组。
I'm wondering what the cleanest way is to implement a cakephp form where 1 control is a multi-select and the rest are text fields or single-selects, and then the data is inserted as multiple rows with a saveall(). So for example a form is selected with these values:
textfield A
value=Foo
mulit-select B
values=US,Mexico,Canada
single=select C
value=10
and so I want to insert these rows into the database with a saveall():
Foo,US,10
Foo,Mexico,10
Foo,Canada,10
Now I know in the add view I can use this format for the input statement:
input('Model.0.field1',...)
but I'm wondering if I can mix that in that same form with inputs formatted like
input('Model.field2',....).
Update:
When I mix and match the single-select and multiple-select controls, the form data gets submitted like this:
Array
(
[Alert] => Array
(
[schedule_id] => 75
[user_id] => 6
[0] => Array
(
[frequency] => Array
(
[0] => WEEKLY
[1] => MONTHLY
)
)
[limit_value] => .03
[limit_adjustment] => 0
[type] => LIMIT
[disabled] => 0
)
)
I tried passing that data into saveall() but it treats it like a single record.
Update2: I think saveAll() requires that the multiple rows of data be formatted like this:
Array
(
[Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
So it looks like after the submit I'm going to need some javascript code that will restructure the array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一些有用的东西......我不确定它是否充分利用了蛋糕的所有“自动魔法”功能,但我不认为它太复杂。
所以我只是将以下代码添加到控制器的 add 函数中:
并将我的数据转换为:
I have something that works... I'm not sure if it takes full advantage of all of cake's "automagic" capabilities, but I don't think it's too convoluted.
So I just added the following code to my controller's add function:
and that converts my data to this: