2012-10-10 45 views
1

我試圖用cakephp 2中的saveAll()方法一次保存大量數據。問題是我不明白我傳遞給saveAll()函數的數組應該是什麼樣子。Cakephp - saveall() - 這個arrray應該是什麼樣的?

我有以下型號:

Recipe: 
hasMany: Recipeitem 
hasAndBelongsToMany:Category 

Category: 
hasAndBelongsToMany: Recipe 

Recipeitem: 
belongsTo: Recipe 
hasMany: Ingredient 

Ingredient: 
belongsTo: Grocery, Recipeitem 

Grocery: 
hasMany: Ingredient 

所以,如果我想保存配方有兩個recipeitems每兩種成分,我應該說我傳遞給白水函數查找數組對象喜歡?

這是我的陣列看起來像此刻:現在

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 
                ) 

              ) 

            ) 

          ) 

        ) 

      ) 

    ) 

+0

[文章我寫了一篇關於它(http://www.pabloleanomartinet.com/cakephp-2-x-saving-and- validate-a-habtm-relation-example /)其他日子..可能有助於:) – pleasedontbelong

回答

0

我已經解決了自己的問題通過閱讀CakePHP的菜譜好一點:

在版本2.1中進行了更改:現在,您可以通過設置$ options ['deep'] = true,將深度關聯數據保存爲 ;

所以對我來說,這是我所需要的:

$saveOptions['deep'] = true; 
if ($this->Recipe->saveAll($this->request->data, $saveOptions)) {