Tabel coding
id | level1 | level2 | title _____________________________________ 1 | 100 | 1 | numberOne 2 | 100 | 2 | NumberTwo 3 | 101 | 1 | Compay 4 | 101 | 2 | association
表Organizations
id | name | type | level ____________________________ 1 | intel | 3 | 1 2 | IBM | 4 | 2
模型Organizations
class organizationsTable extends table
{
public function initialize(array $config)
{
$this->table('organizations');
$this->primaryKey('id');
$this->belongsTo('coding', [
'foreignKey' => 'type',
'joinType' => 'INNER',
],
'coding', [
'foreignKey' => 'level',
'joinType' => 'INNER']);
}
}
此代码$organization->find('all')->contain(['coding'])
仅将level
设置为foreignKey,但我希望将level
和type
设置为foreignKey for table organizations
像我这样的SQL查询
select * from organizations
inner join coding
on organizations.type=coding.id
inner join coding ccc
on organizations.level=ccc.id
如何创建这样的连接?
修改
我写这个,但说错误:organizations is not associated with coding
$this->belongsTo('yyyy', [
'foreignKey' => 'type',
'className' => 'coding'
],
'xxxx', [
'foreignKey' => 'level',
'className' => 'coding'
]);
答案 0 :(得分:3)
在cakephp2 Multiple relations to the same model
中但是在cakephp3中尝试这样
$this->belongsTo('aliasName1', [
'foreignKey' => 'type',
'className' => 'coding'
]);
$this->belongsTo('aliasName2', [
'foreignKey' => 'level',
'className' => 'coding'
]);
控制器
$organization->find('all')->contain(['aliasName1'])->contain(['aliasName2']);