php - CakePHP - How to add associated records to a model on save? -


i'm using cakephp 2.3 , i'm trying find correct way perform associated data saving. i've setup image upload form save picture data in mean time want process saved image such on save application should examine file , extract exif data , add tags associated models tags.

my relations are

picture hasmany tags tag belongsto picture 

right i'm trying use picture beforesave callback programmatically add new records data array it's not working (it's not saving added data).

picturecontroller

$this->picture->create();            $this->picture->saveall($this->request->data); 

data array after beforesave

array (     [picture] => array         (             [field1] => foo             [field2] => bar         )      [tag] => array         (             [0] => array                 (                     [tag] => example                     [value] => example                 )              [1] => array                 (                     [tag] => example 2                     [value] => example 2                 )          ) ) 

how can achieve want without messing controller? business logic stay on model.

you need use deep in order achieve this. according cookbook:

to save associated data $options['deep'] = true (since 2.1)

examples

$model->savemany($data, array('deep' => true)); $model->saveassociated($data, array('deep' => true)); 

check cookbook more details


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -