Cakephp 3.x 保存 hasmany 关联

Posted

技术标签:

【中文标题】Cakephp 3.x 保存 hasmany 关联【英文标题】:Cakephp 3.x saving hasmany association 【发布时间】:2016-11-10 14:37:27 【问题描述】:

在我的项目中,我无法在一次保存中保存多个关联表。我的联想如下

类 ProductsTable 扩展表

$this->hasMany('ProductImages', [
            'foreignKey' => 'product_id',
            'joinType' => 'INNER'
        ]);

类 ProductImagesTable 扩展表

$this->belongsTo('Products', [
        'foreignKey' => 'product_id',
        'joinType' => 'INNER'
    ]);

在我的控制器中代码如下

debug($this->request->data);  \src\Controller\Admin\ProductsController.php (line 81)

$product = $this->Products->patchEntity($product, $this->request->data, [
    'associated' => [ 'ProductImages']
]); 
debug($product);\src\Controller\Admin\ProductsController.php (line 86)
$this->Products->save($product);

调试结果如下

 \src\Controller\Admin\ProductsController.php (line 81)

[
    'name' => 'Lorem ipsum dolor',
    'short_description' => ' Lorem ipsum dolor sit amet',
    'description' => ' Lorem ipsum dolor sit amet ',
    'sku' => 'NE132W',
    'price' => '51',
    'product_image' => [
        (int) 0 => [
            'default' => (int) 0,
            'image_name' => 'product1',
            'real_name' => '631-1478785843.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/631-1478785843.png'
        ],
        (int) 1 => [
            'default' => '1',
            'image_name' => 'product2',
            'real_name' => '140-1478785850.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/140-1478785850.png'
        ],
        (int) 2 => [
            'default' => (int) 0,
            'image_name' => 'product3',
            'real_name' => '416-1478785856.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/416-1478785856.png'
        ]
    ]
]

\src\Controller\Admin\ProductsController.php (line 86)

object(App\Model\Entity\Product) 

    'name' => 'Lorem ipsum dolor',
    'short_description' => ' Lorem ipsum dolor sit amet',
    'description' => ' Lorem ipsum dolor sit amet, consectetur adipiscing elit.  ',
    'sku' => 'NE132W',
    'price' => (float) 51,
    'product_image' => [
        (int) 0 => [
            'default' => (int) 0,
            'image_name' => 'product1',
            'real_name' => '631-1478785843.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/631-1478785843.png'
        ],
        (int) 1 => [
            'default' => '1',
            'image_name' => 'product2',
            'real_name' => '140-1478785850.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/140-1478785850.png'
        ],
        (int) 2 => [
            'default' => (int) 0,
            'image_name' => 'product3',
            'real_name' => '416-1478785856.png',
            'image_url' => 'http://cakephp-apps.com/mykipferl/img/Products/416-1478785856.png'
        ]
    ],
    '[new]' => true,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [
        'name' => true,
        'short_description' => true,
        'description' => true,
        'sku' => true,
        'price' => true,
        'product_image' => true
    ],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Products'


有人帮我解决这个问题吗?

【问题讨论】:

【参考方案1】:

您必须为关联使用适当的属性名称,对于 hasMany 关联,默认情况下是关联别名的复数、带下划线的变体,因此在您的情况下是 product_images,而不是 product_image

另见

Cookbook > Database Access & ORM > Associations > HasMany Associations Cookbook > Database Access & ORM > Saving Data > Saving HasMany Associations Cookbook > Views > Helpers > Form > Field Naming Conventions

【讨论】:

感谢您的回复,关键字“product_images”确实对我有用。还有一点很重要(对不起,这是我的错误)不要在表中的字段名称中使用特殊关键字。正如您在我的调试中看到的那样,product_images 表中有一个字段名称“default”会导致错误。最后,仅当我更改字段值“默认”时,我的保存才有效:) @ndm,CakePHP 是否支持自动更新 hasMany 关联的记录。例如-我有 2 个产品属于类别 1。当我编辑类别 1 并删除 1 个产品并尝试通过类别模型保存数据时,它会从数据库中删除产品 2 吗?请查看我的问题以获取详细信息***.com/questions/47739248/…

以上是关于Cakephp 3.x 保存 hasmany 关联的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP - 如何一次通过记录保存单个 hasMany,每个 hasMany 有多个 belongsTo 记录?

CakePHP hasAndBelongsToMany vs hasMany 通过

CakePHP如何在一个saveAll中不知道外键的情况下创建hasMany数据?

CakePHP - 在模型中查找没有 hasMany 关系的 hasMany 关联?

CakePHP:无法识别 hasMany 关联

CakePHP HABTM 关系保存新的关联