Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saveAll() on nested data

When I try to create a new entry of Template with the $data array, the models of Template, Group and Product are saved correctly. But the nested Calcfield (which is a hasMany of Group) is not saved. :( Group and Product are hasMany of Template.

Is this possible at all?

$this->Template->create();
$this->Template->saveAll($data, array('validate' => false));

// $data looks like this:

Array
(
    [Template] => Array
        (
            [title] => 
            [shorttitle] => Wie auch immer
            [place] => Hannover
            [size] => 
        )

    [Group] => Array
        (
            [0] => Array
                (
                    [title] => Hosting
                    [order] => 
                    [Calcfield] => Array
                        (
                            [0] => Array
                                (
                                    [title] => Hosting
                                    [value] => 0
                                    [description] => 
                                )
                            [1] => Array
                                (
                                    [title] => Strom
                                    [value] => 0
                                    [description] => 
                                )
                        )
                )
        )

    [Product] => Array
        (
            [0] => Array
                (
                    [share] => 10
                    [businessunit] => Marketing
                )
            [1] => Array
                (
                    [share] => 30
                    [businessunit] => intl. CRM
                )
        )
)
like image 837
powtac Avatar asked Aug 03 '26 05:08

powtac


2 Answers

No, recursive saves are not possible, as far as I know. You would need to stick those in a separate array and then save them after the initial save.

like image 196
Barry Chapman Avatar answered Aug 05 '26 20:08

Barry Chapman


Since CakePHP 2.1 it's possible to save deeply-nested models using the deep parameter in saveAll (since CakePHP 2.1)

ref. http://book.cakephp.org/2.0/en/models/saving-your-data.html?highlight=saveall#model-saveassociated

like image 23
Nicola Beghin Avatar answered Aug 05 '26 20:08

Nicola Beghin