1

我正在尝试使用 cakephp 2 中的 saveAll() 方法一次保存大量数据。问题是我不明白我传递给 saveAll() 函数的数组应该是什么样子。

我有以下型号:

Recipe:
hasMany: Recipeitem
hasAndBelongsToMany:Category

Category:
hasAndBelongsToMany: Recipe

Recipeitem:
belongsTo: Recipe
hasMany: Ingredient

Ingredient:
belongsTo: Grocery, Recipeitem

Grocery:
hasMany: Ingredient

因此,如果我想保存一个包含两个配方项的配方,每个配方项有两种成分,那么我传递给 saveAll 函数的数组对象应该是什么样的?

这是我的数组目前的样子:

Array
(
[Recipe] => Array
    (
        [title] => Mushroom pie
        [instructions] => Just mix it!
        [cooking_time] => 20
    )

[Category] => Array
    (
        [Category] => Array
            (
                [0] => 5
                [1] => 3
            )

    )

[Recipeitem] => Array
    (
        [0] => Array
            (
                [Recipeitem] => Array
                    (
                        [name] => Crust
                        [order] => 0
                        [Ingredients] => Array
                            (
                                [0] => Array
                                    (
                                        [Ingredient] => Array
                                            (
                                                [amount] => 2
                                                [unit] => dl
                                                [order] => 0
                                                [Grocery] => Array
                                                    (
                                                        [name] => Butter
                                                        [description] => Butter
                                                    )

                                            )

                                    ),
                                [1] => Array
                                    (
                                        [Ingredient] => Array
                                            (
                                                [amount] => 3
                                                [unit] => dl
                                                [order] => 1
                                                [Grocery] => Array
                                                    (
                                                        [name] => Sugar
                                                        [description] => Sugar
                                                    )

                                            )

                                    )

                            )

                    )

            ),
        [1] => Array
            (
                [Recipeitem] => Array
                    (
                        [name] => Filling
                        [order] => 1
                        [Ingredients] => Array
                            (
                                [0] => Array
                                    (
                                        [Ingredient] => Array
                                            (
                                                [amount] => 2
                                                [unit] => dl
                                                [order] => 0
                                                [Grocery] => Array
                                                    (
                                                        [name] => Mushroom
                                                        [description] => Mushroom
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

4

1 回答 1

0

我现在通过更好地阅读 CakePHP 食谱自己解决了这个问题:

在 2.1 版更改: 您现在可以通过设置 $options['deep'] = true; 来保存更深层次的关联数据;

所以对我来说,这就是我所需要的:

$saveOptions['deep'] = true;
if ($this->Recipe->saveAll($this->request->data, $saveOptions )) {
于 2012-10-17T19:17:53.107 回答