belongsTo在cakephp3中不起作用

时间:2015-03-31 07:32:36

标签: php sql cakephp foreign-key-relationship

organatinos与organType有很多关系

Tabel组织

id   |  name  | organType_id

1    |  a     | 1

2    |  b     | 1

3    |  c     | 2

4    |  d     | 3

表organType

id   |  name  

1    |  aa     

2    |  bb    

3    |  cc 

我在organiztinos模型中使用belongsTo=>'organType'

模特组织

class organizationsTable extends table
{
    public function initialize(array $config)
    {
        $this->table('organizations');
        $this->primaryKey('id');

        $this->belongsTo('organType', [
           'foreignKey' => 'organType_id',            
        ]);

    }
}

模型organType

class organTypeTable extends table
{
    public function initialize(array $config)
    {    
        $this->table('organType');
        $this->primaryKey('id');
    }
}

控制器

$organization=TableRegistry::get('organizations');
$organization=$organization->find('all')->offset(0)->limit(30) ;  
debug($organization);

但是在结果中只返回组织表我希望组织内部联接与organ_type

结果不好:

'sql' => 'SELECT organizations.id AS `organizations__id`, organizations.name AS `organizations__name`, organizations.organType_id AS `organizations__organType_id` FROM organizations organizations LIMIT 30 OFFSET 0',

我想要这个结果:

 'sql' => 'SELECT organizations.id , organizations.name , organizations.organType_id FROM organizations inner join organType on organizations.organType_id=organType.id

1 个答案:

答案 0 :(得分:1)

book.cakephp.org

用于从cakephp 3中的外表中选择使用contain(['organType'])

$organization=$organization->find('all')->contain(['organType'])->offset(0)->limit(30) ;