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

では、それぞれ 2 つの材料を含む 2 つのレシピ項目を持つレシピを保存したい場合、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 に答える