简体   繁体   English

CakePHP保存hasMany关联

[英]CakePHP Saving hasMany association

i've a product that needs to save an image path to another table. 我有一个需要将图像路径保存到另一个表的产品。 product hasMany images . product hasMany images

This is my add() 这是我的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. 这是表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. 它只是没有为此触发SQL语句,而我对为什么要注意, hasOne assoc感到困惑。 does work, so I can save assoc. 确实有效,所以我可以保存assoc。 but not with this hasMany 但是没有这个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). 您正在将图像作为数组添加到数据中,这是行不通的,ORM将仅保存实体对象,即您必须创建一个\\Cake\\Datasource\\EntityInterface实例(这是实体创建/修补的过程)进程将自动处理传递的数据)。

$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). 另外,您还需要确保将images属性标记为脏,否则ORM会忽略它(在实体创建/修补过程中也会自动完成)。 Your isDirty(true) call won't do anything, as isDirty() isn't a setter, but a getter. 您的isDirty(true)调用不会执行任何操作,因为isDirty()不是设置方法,而是获取方法。

$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. 另外,最好不要使用debug()或至少使用var_export()来记录JSON表示形式,以便保留实体提供的调试信息。

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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