Cakephp:关于带有多选的 saveall() 的问题

发布于 2024-10-14 18:50:38 字数 1404 浏览 3 评论 0原文

我想知道最简洁的方法是实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-10-21 18:50:39

我有一些有用的东西......我不确定它是否充分利用了蛋糕的所有“自动魔法”功能,但我不认为它太复杂。

所以我只是将以下代码添加到控制器的 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));
        }

并将我的数据转换为:

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
                )

        )

)

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:

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
                )

        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文