Wednesday, October 17, 2012

Re: how do I use saveAll()?

I've solved the problem myself now by reading the CakePHP cookbook a little better:

Changed in version 2.1: You can now save deeper associated data as well with setting $options['deep'] = true;

So for me this was all I needed:

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

Den måndagen den 15:e oktober 2012 kl. 21:08:16 UTC+2 skrev Gustav Persson:
Hii,

I'm trying to use the saveAll() method to create and save related data in my CakePHP (2.2.1) app. I have a form where the user can input recipe information and create recipeitems and ingredients. The problem is that I don't know how to store the ingredients data.

I use the saveAll() method in the RecipeController, and this has worked to save the Reipce, Categories and RecipeItems, but NOT the Ingredients or Groceries.

Is it possible to save all this data at once? If so, what should the input data to saveAll look like?

All help is greatly appreciated!




My relationships look like this:
Recipe:
hasMany
: Recipeitem
hasAndBelongsToMany
:Category

Category:
hasAndBelongsToMany
: Recipe

Recipeitem:
belongsTo
: Recipe
hasMany
: Ingredient

Ingredient:
belongsTo
: Grocery, Recipeitem

Grocery:
hasMany
: Ingredient

The data I'm passing to saveAll() by $this->Recipe->saveAll($this->request->data):
Array  (      [Recipe] => Array          (              [title] => Tomato pie              [description] => A tasty pie              [cooking] => <p>Mix everything!</p>            )        [Category] => Array          (              [Category] => Array                  (                      [0] => 5                      [1] => 3                  )            )        [Recipeitem] => Array          (              [0] => Array                  (                      [Recipeitem] => Array                          (                              [name] => Deg                              [order] => 0                              [Ingredient] => Array                                  (                                      [0] => Array                                          (                                              [Ingredient] => Array                                                  (                                                      [amount] => 2                                                      [unit] => dl                                                      [order] => 0                                                      [grocery_id] => 1                                                  )                                            )                                    )                            )                    )            )    )

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: