PatchEntity 在 Cakephp 3.0 中忽略来自 ajax 请求的数据

PatchEntity ignore data from an ajax request in Cakephp 3.0

我经常使用 patchEntity 函数将我的实体与表单数据混合,它工作正常,即使有 ajax 请求。

但是,当我尝试从 ajax 请求中插入带有 JSON 数据的数据时,patchEntity 无法检索数据。

我的ajax要求很简单:

var rate = function (user, rate, success, error) {
    $.ajax({
            type: "POST",
            url: baseUrl + 'rate/add',
            data: {
                id: this.id,
                user: user.id
                rate: rate
            },
            dataType: 'json',
            success: success,
            error: error
        });
});

在我的速率控制器中,我的添加函数如下所示:

 public function add()
 {
     if ($this->request->isAjax()) {
        $this->layout = 'ajax';
        $rate = $this->Rate->newEntity();
        if ($this->request->is('post')) {
            $rate = $this->Rate->patchEntity($rate, $this->request->data);
           if ($rate->errors()) {
                $this->set([
                    'status' => 500,
                    'message' => $rate->errors()
                ]);
            } else {
                if ($this->rate->save($rate)) {
                    $this->set([
                        'status' => 200
                    ]);
                } else {
                    $this->set([
                        'status' => 500,
                        'message' => $rate->errors()
                    ]);
                }
            }
            return $this->render('/Ajax/result');
        }
}

这会引发异常:

Cannot insert row, some of the primary key values are missing. Got (, , ), expecting (id, user)

我可以用这个代替 $this->Rate->patchEntity($rate, $this->request->data);

来保存我的数据
$rate['id'] = $this->request->data['id'];
$rate['user'] = $this->request->data['user'];
$rate['rate'] = $this->request->data['rate'];

我必须向 patchEntity 函数传递什么样的数组才能使它起作用?

感谢 ndm 的评论,我检查了 Rate Entity 并删除了由 bake 自动生成的这部分:

protected $_accessible = [
    'rate' => true,
];