php - CakePHP Saving hasMany association -


i've product needs save image path table. product hasmany images.

this add()

if ($this->request->is('post')) {     // https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-associations     $save = $this->products->newentity(         $this->request->getdata(),         ['associated'=>$associated]     );      $this->log('just after patch'.$save);     $save->last_modified = time::now();     $save->created = time::now();      //sort out assoc.     $path = $this->saveimagegetpath($this->request->data['images']);      $save['images'][] = [         'path' => $path,         'alt_description' => $product->name . time::now() . 'img',         'position' => $this->request->data['position']     ];      $save->isdirty(true);     $this->log('just before save'.$save);     if ($this->products->save($save, [         'associated'=> ['shoes', 'images', 'products_has_categories']     ])) { 

this array output log

{     "name":"xlc",     "brands_id":1,     "description":"csvfd",     "type":"s",     "position":"0",     "shoe":{         "heels_id":1,         "closures_id":1,         "materials_upper_id":1,         "materials_lining_id":1,         "materials_sole_id":1     },     "products_has_categories":{         "categories_id":"1"     },     "images":[         {             "path":"\/webroot\/img\/products\/img1503289958.jpg",             "alt_description":"8\/21\/17, 4:32 amimg",             "position":"0"         }     ],     "last_modified":"2017-08-21t04:32:38+00:00",     "created":"2017-08-21t04:32:38+00:00" } 

this table image assoc.

$this->hasmany('images', [     'foreignkey' => 'products_id',     'dependent' => true, ]); 

the images upload fine , can path. it's not triggering sql statement , i'm baffled why note, hasone assoc. work, can save assoc. not hasmany

you're adding image array data, won't work, orm save entity objects, ie have create instance of \cake\datasource\entityinterface instead (that's entity creation/patching process automatically passed data).

$save['images'][] = $this->products->images->newentity([     'path' => $path,     'alt_description' => $product->name . time::now() . 'img',     'position' => $this->request->data['position'] ]); 

also need make sure images property being marked dirty, otherwise orm ignore (that's being done automatically in entity creation/patching process). isdirty(true) call won't anything, isdirty() isn't setter, getter.

$save->setdirty('images', true); // use dirty('images', true) in cakephp < 3.4 

also instead of logging json representations, better use debug(), or @ least var_export() instead, in order retain debugging information entity provides.

see also


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -