Cakephp 3.x hasOne 插入默认外键
Cakephp 3.x hasOne inserts default foreign key
我有一个 table X 通过包含连接到 table Y
当我想使用 hasOne 和函数将 this 与 table Z 进行内部连接(匹配)时。
CakePHP 通过 hasOne
自动连接到不存在的默认行
public function initialize(array $config)
{
$this->belongsTo('Y', [
'bindingKey' => 'initialen',
'foreignKey' => 'initialen'
]);
$this->hasOne('Z');
}
进一步
public function search($c)
{
$query = $this->find('all')->contain('Y')->matching('Z', function ($q) use ($c) {
return $q->where(['Z.client_ID' => $c]);
});
return $query;
}
我遇到错误
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column
'Z.search_id' in 'on clause'
如果Z.search_id不存在,你必须指定其他确实存在的外键:
$this->hasOne('Z',[
'foreignKey' => 'some_key'
]);
好的,我自己找到了解决方案
$this->hasOne('Z',[
'foreignKey' => false
]);
我有一个 table X 通过包含连接到 table Y 当我想使用 hasOne 和函数将 this 与 table Z 进行内部连接(匹配)时。 CakePHP 通过 hasOne
自动连接到不存在的默认行 public function initialize(array $config)
{
$this->belongsTo('Y', [
'bindingKey' => 'initialen',
'foreignKey' => 'initialen'
]);
$this->hasOne('Z');
}
进一步
public function search($c)
{
$query = $this->find('all')->contain('Y')->matching('Z', function ($q) use ($c) {
return $q->where(['Z.client_ID' => $c]);
});
return $query;
}
我遇到错误
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Z.search_id' in 'on clause'
如果Z.search_id不存在,你必须指定其他确实存在的外键:
$this->hasOne('Z',[
'foreignKey' => 'some_key'
]);
好的,我自己找到了解决方案
$this->hasOne('Z',[
'foreignKey' => false
]);