2015-04-07 42 views
0

coding TABELcakephp的3模型不相关的误差

 

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('yyyy', [ 
       'foreignKey' => 'type', 
       'className' => 'coding'  
       ], 
       'xxxx', [ 
       'foreignKey' => 'level', 
       'className' => 'coding' 
      ]); 

    } 
} 

控制器

$organization->find('all')->contain(['yyyy'])->contain(['xxxx'])->toList() ; 

我写这篇文章,但说错误:organizations is not associated with xxxx

怎么办多重关系到cakephp3相同的模型?

回答

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