如何在Cake 3.0中保存belongsToMany数据?

use*_*667 2 php cakephp has-and-belongs-to-many cakephp-3.0

我是 cakephp 的新手。文档位于http://book.cakephp.org/3.0/en/orm/associations.html#belongstomany-associationshttp://book.cakephp.org/3.0/en/orm/ saving-data.html# saving对于像我这样的初学者来说,-with-associations似乎要么太简短,要么太高级。

据我所知,我做了以下事情。

//Table Baskets belongsToMany Apples
//At BasketsTable.php in initialize()
$this->belongsToMany('Apples',[
            'joinTable' => 'apples_baskets'
        ]);
Run Code Online (Sandbox Code Playgroud)

MySQL 中的连接表apples_baskets

+---+---------+----------+
|id |apple_id |basket_id |
--------------------------
|   |         |          |
--------------------------
Run Code Online (Sandbox Code Playgroud)

我已将发布请求数据显示在控制器上,如下所示:

Array
(
    [id] => 1
    [xyz] => blahblah
    [apples] => Array
        (
            [_ids] => Array
                (
                    [0] => 1
                )

        )

    [amount] => 15000
)
Run Code Online (Sandbox Code Playgroud)

现在,当我执行 时save,只有 Baskets 表得到更新,连接表保持不变,并且不会引发错误。

我知道我肯定错过了一些东西,但无法弄清楚。请帮忙 !!!

bep*_*ter 5

尝试检查您的Baskets实体类是否具有其apples属性_accessible

<?php
namespace app\Model\Entity;

use Cake\ORM\Entity;

class Basket extends Entity {
    public $_accessible = [
        'apples', // <-- make sure this is present
    ];
}
Run Code Online (Sandbox Code Playgroud)

在使用这些新的实体类时,一个非常常见的问题是尝试向它们加载数据(使用Table::newEntity()Table::patchEntity()),但由于批量分配保护,其中一些数据实际上并未保存到实体中。

参考:http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment