我正在使用 CakePHP 4 创建一个简单的 API,但在保存一些 POST 数据时遇到了一些问题。这是一个新项目,因此模型是新烘焙的,基于我的 3 个表:elaborations
、details
、jobs
ElaborationTable
表具有以下关系:
$this->hasOne('Details', [ // this (and the next) is the only thing that I've manually changed here: hasOne instead of hasMany
'foreignKey' => 'elaboration_id',
]);
$this->hasOne('Jobs', [
'foreignKey' => 'elaboration_id',
]);
虽然 DetailsTable
和 JobsTable
都有这个:
$this->belongsTo('Elaborations', [
'foreignKey' => 'elaboration_id',
'joinType' => 'INNER',
]);
在我的控制器中,这是我用来检索 POST 数据并将其保存到每个数据库表中的内容:
$data = $this->request->getData();
$data['user_id'] = $this->Auth->user('id');
$elaboration = $this->Elaborations->newEntity($data, [
'associated' => [
'Details',
'Jobs'
]
]);
最后,我的 POST 数据是这样的:
APP/Controller/ElaborationsController.php (line 85)
[
'detail' => [
'regione' => 'Lazio',
'provincia' => 'RM',
'comune' => 'Roma',
],
'job' => [
'serramenti' => '1',
],
'user_id' => (int) 2,
]
现在,user_id
字段保存在我的 elaborations
表中,没有发出错误,但 details
和 jobs
表仍然为空,没有保存任何数据.
我做错了什么?