简体   繁体   中英

CakePHP Saving hasMany association

i've a product that needs to save an image path to another table. product hasMany images .

This is my 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 is the array output to the 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 is the table Image assoc.

$this->hasMany('Images', [
    'foreignKey' => 'products_id',
    'dependent' => true,
]);

The images upload fine and you can get the path. It's just not triggering a SQL statement for this and i'm baffled as to why Do note, that a hasOne assoc. does work, so I can save assoc. but not with this hasMany

You're adding the image as an array to the data, that won't work, the ORM will only save entity objects, ie you have to create an instance of \\Cake\\Datasource\\EntityInterface instead (that's what the entity creation/patching process will do automatically for the passed data).

$save['images'][] = $this->Products->Images->newEntity([
    'path' => $path,
    'alt_description' => $product->name . Time::now() . 'img',
    'position' => $this->request->data['position']
]);

Also you need to make sure that the images property is being marked as dirty, as otherwise the ORM will ignore it (that's also being done automatically in the entity creation/patching process). Your isDirty(true) call won't do anything, as isDirty() isn't a setter, but a getter.

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

Also instead of logging JSON representations, you better use debug() , or at least var_export() instead, in order to retain the debugging information that the entity provides.

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM