我正在学习 cakePHP 3.0,在我的模型上保存相关数据时遇到了一些问题。
我尝试使用 ClientPreferences 的关联数据保存客户端
客户表
class ClientsTable extends Table
{
public function initialize(array $config)
{
(...)
$this->belongsTo('ClientPreferences', [
'foreignKey' => 'client_preferences_id'
]);
}
}
客户端控制器
$aClient = $this->Clients->newEntity();
$aClient = $this->Clients->patchEntity($aClient, $this->request->data);
$aClientPreference = $this->Clients->ClientPreferences->newEntity();
$aClientPreference->my_field = 'my value';
$aClient->ClientPreferences = $aClientPreference;
$this->Clients->save($aClient, ['associated' => ['ClientPreferences']];
Client 实体已正确保存,但未正确保存关联的 ClientPreferences 实体,并且 Cake 没有引发错误。
我试图遵循这个: http ://book.cakephp.org/3.0/en/orm/saving-data.html#saving-with-associations
但是没有发现任何问题可以正确执行。有人有建议吗?
先感谢您。