我正在使用 CakePHP 3.4
我正在检索用户的信息,例如
$user = $this->Users->get($id, [
'contain' => [
'UserAddresses.States.Countries' => ['conditions' => [
'UserAddresses.deleted' => false]],
],
]);
在这里,UserAddresses
有user_id
列和state_id
列,States
表有country_id
列。
如果表中没有关联的 user_address,那么
即使在不存在记录的情况下,我也会在从作品中正常record not found
移除时给出错误States.Countries
contain
UserAddresses
编辑 2:关系
用户表.php
$this->hasOne('UserAddresses', [
'foreignKey' => 'user_id'
]);
用户地址表.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('States', [
'foreignKey' => 'state_id',
'joinType' => 'INNER'
]);
状态表.php
$this->belongsTo('Countries', [
'foreignKey' => 'country_id',
'joinType' => 'INNER'
]);
$this->hasMany('UserAddresses', [
'foreignKey' => 'state_id'
]);
国家表.php
$this->hasMany('States', [
'foreignKey' => 'country_id'
]);