【发布时间】:2016-10-18 18:59:38
【问题描述】:
你好,我有一个表名University_Industry。它有uni_id 和ind_id 作为University 表和Industry 表的外键。所以意味着大学会有很多行业,行业会有很多大学。我通过运行此查询成功获得了结果
public function getAllUniv(){
return $this->find('all', array(
'order' => 'uni_id',
));
}
此查询返回大学及其行业。结果如下所示
Array
(
[0] => Array
(
[University] => Array
(
[uni_id] => 1
[uni_name] => NYC
)
[Industry] => Array
(
[0] => Array
(
[ind_id] => 1
[ind_name] => Finance
[UniversityIndustry] => Array
(
[id] => 1
[uni_id] => 1
[ind_id] => 1
)
)
[1] => Array
(
[ind_id] => 2
[ind_name] => Accounting
[UniversityIndustry] => Array
(
[id] => 2
[uni_id] => 1
[ind_id] => 2
)
)
)
)
现在我有另一个名为 users 的表,其中 uni_id 也作为外键。所以我正在寻找一个查询,它只能获取那些在用户表中找到的行业的大学。所以它应该只获取那些 uni_id 存在于 users 表中的大学
我不知道如何在查询中加入 users 表并获得结果。
这就是我现在的大学模式的样子
class University extends AppModel
{
public $useTable = 'university';
public $primaryKey = 'uni_id';
public $hasAndBelongsToMany = array(
'Industry' =>
array(
'className' => 'Industry',
'joinTable' => 'university_industry',
'foreignKey' => 'uni_id',
'associationForeignKey' => 'ind_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
),
);
public function getAllUniv(){
return $this->find('all', array(
'order' => 'uni_id',
));
}
}
【问题讨论】:
标签: cakephp cakephp-2.0 cakephp-2.3