Cakephp HasMany + SaveAll doesnt work

2019-05-30 09:37发布

问题:

Im trying to do a hasmany saveall() but it does not work.

I have a Model Carmodel hasMany CarmodelsImage When i try to save, the array passed is:

[CarmodelsImage] => Array
    (
        [0] => Array
            (
                [name] => teste
                [carmodel_id] => 1
            )
    )

In the controller i have $this->Carmodel->saveAll($this->request->data) but it does not work.

I need some help.

I know this question was already posted but I read every answers and it not work.

Thanks

回答1:

Your requested data needs to be an array like in the following code:

Array
(
   [Carmodel] => Array
                 (
                       //Carmodel fields here
                 )
   [CarmodelsImage] => Array
                      (
                       [0] => Array
                             (
                               [name] => teste
                               [carmodel_id] => 1
                             )

                       [1] => Array
                             (
                               [name] => abc
                               [carmodel_id] => 2
                             )
                       ..........
                     )
)

$this->Carmodel->saveAll($this->request->data, array('deep' => true));

You have to use 'deep' => true option with saveAll() method while saving associated models details.