[英]Cakephp: question about saveall() with multiselect
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(). 我想知道最简单的方法是实现一个Cakephp形式,其中1个控件是多选,其余控件是文本字段或单选,然后使用saveall()将数据插入为多行。 So for example a form is selected with these values:
因此,例如,选择了具有这些值的表单:
textfield A value=Foo 文本字段A值= Foo
mulit-select B values=US,Mexico,Canada 多重选择B值=美国,墨西哥,加拿大
single=select C value=10 单次=选择C值= 10
and so I want to insert these rows into the database with a saveall(): Foo,US,10 Foo,Mexico,10 Foo,Canada,10 所以我想使用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',...) 输入('Model.0.field1',...)
but I'm wondering if I can mix that in that same form with inputs formatted like input('Model.field2',....). 但我想知道是否可以以相同的形式将其与输入格式如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. 我尝试将数据传递到saveall(),但将其视为单个记录。
Update2: I think saveAll() requires that the multiple rows of data be formatted like this: Update2:我认为saveAll()要求数据的多行格式如下:
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. 所以看起来在提交之后,我将需要一些javascript代码来重组数组。
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: 因此,我将以下代码添加到了控制器的add函数中:
if (!empty($this->data)) {
//debug($this->data, true);
/* begin custom code */
$multiselect = $this->data['Alert']['entity_id'];
$tmp2 = array();
foreach ($multiselect as $item)
{
$tmp = $this->data['Alert'];
$tmp['entity_id'] = $item;
array_push($tmp2,$tmp);
}
$this->data['Alert'] = $tmp2;
debug($this->data,true);
/* end custom code */
$this->Alert->create();
//restructure data
if ($this->Alert->saveAll($this->data['Alert'])) {
$this->Session->setFlash(__('The alert has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The alert could not be saved. Please, try again.', true));
}
and that converts my data to this: 然后将我的数据转换为此:
Array
(
[Alert] => Array
(
[0] => Array
(
[schedule_id] => 74
[entity_id] => 1
[user_id] => 6
[frequency] => HOURLY
[limit_value] => .02
[limit_adjustment] => 0
[type] => LIMIT
[disabled] => 1
)
[1] => Array
(
[schedule_id] => 74
[entity_id] => 2
[user_id] => 6
[frequency] => HOURLY
[limit_value] => .02
[limit_adjustment] => 0
[type] => LIMIT
[disabled] => 1
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.