cakephp3两个外键同型号

cakephp3两个外键同型号

问题描述:

Tabel coding


id |  level1  |  level2  |  title
_____________________________________
1  |  100     |  1       |  numberOne
2  |  100     |  2       |  NumberTwo
3  |  101     |  1       |  Compay
4  |  101     |  2       |  association

Table Organizations

id  | name  |  type  | level
____________________________
1   | intel |  3     |  1 
2   | IBM   |  4     |  2

Model 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']);

    }
}

this code$organization->find('all')->contain(['coding']) only set level to foreignKey but i want set level and type to foreignKey for table organizations

i sql query like this

select * from organizations inner join coding on organizations.type=coding.id inner join coding ccc on organizations.level=ccc.id

how create join like this?

EDIT

i write this but say error :organizations is not associated with coding

$this->belongsTo('yyyy', [
    'foreignKey' => 'type',
    'className' => 'coding' 
    ],
    'xxxx', [
    'foreignKey' => 'level',
    'className' => 'coding'
]);

Tabel coding code> p>

 
 
id  |  level1 |  level2 |  title 
 _____________________________________ 
1 |  100 |  1 |  numberOne 
2 |  100 |  2 |  NumberTwo 
3 |  101 |  1 |  Compay 
4 |  101 |  2 | 关联
  pre> 
 
 

组织 code> p>

 
id | 名字| 类型| 等级
 ____________________________ 
1 | 英特尔|  3 |  1 
2 |  IBM |  4 |  2 
  pre> 
 
 

模型组织 code> p>

  class organizationsTable extends table 
 {
 public function initialize(  array $ config)
 {
 $ this-> table('organizations'); 
 $ this-> primaryKey('id'); 
 
 $ this-> belongsTo('coding',  [
'foreignKey'=>'type',
'joinType'=>'INNER',
],
'编码',[
'foreignKey'=>'级别',
  'joinType'=>'INNER']); 
 
} 
} 
  code>  pre> 
 
 

此代码 $ organization-> find(' all') - >包含(['coding']) code>只将 level code>设置为foreignKey但我想要设置 level code>和 type code > to foreignKey for table organizations code> p>

我这样的SQL查询 p>

select * from organizations inner join coding on organizations.type = coding.id inner join coding ccc on organizations.level = ccc.id code> p>

如何创建这样的连接? p> \ ñ

编辑 strong> p>

我写这个但是说错误:组织与编码无关 code> p>

   $ this-> belongsTo('yyyy',[
'foreignKey'=>  'type',
'className'=>  'coding'
],
'xxxx',[
'foreignKey'=>  'level',
'className'=>  'coding'
]); 
  code>  pre> 
  div>

in cakephp2 Multiple relations to the same model

but in cakephp3 try like this

$this->belongsTo('aliasName1', [
    'foreignKey' => 'type',
    'className' => 'coding'
    ]);
$this->belongsTo('aliasName2', [
    'foreignKey' => 'level',
    'className' => 'coding' 
    ]);

Controller

$organization->find('all')->contain(['aliasName1'])->contain(['aliasName2']);