HABTM 加入 Cakephp
HABTM with join Cakephp
你好,我有一个 table 名字 University_Industry
。它有 uni_id
和 ind_id
作为 University table 和 Industry table 的外键.所以意思是大学会有很多行业,行业会有很多大学。我通过 运行 这个查询
成功地得到了结果
public function getAllUniv(){
return $this->find('all', array(
'order' => 'uni_id',
));
}
此查询 returns 我的大学及其 industries.Result 看起来像这样
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 的 table,它也有 uni_id
作为外键。所以我正在寻找一个查询,它只能获取那些在用户 table 中找到的行业的大学。所以它应该只获取用户 table
中存在 uni_id 的那些大学
我不知道如何在我的查询中加入用户 table 并获得结果。
这就是我的大学模态现在的样子
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',
));
}
}
您可以在查找查询中加入数据。
$this->find('all', array(
'order' => 'uni_id',
'joins' => array(
array(
'table' => 'users ',
'alias' => 'UserJoin',
'type' => 'inner',
'conditions' => array(
'UserJoin.uni_id = University.id'
)
),
));
这里是official doc。希望对您有所帮助
你好,我有一个 table 名字 University_Industry
。它有 uni_id
和 ind_id
作为 University table 和 Industry table 的外键.所以意思是大学会有很多行业,行业会有很多大学。我通过 运行 这个查询
public function getAllUniv(){
return $this->find('all', array(
'order' => 'uni_id',
));
}
此查询 returns 我的大学及其 industries.Result 看起来像这样
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 的 table,它也有 uni_id
作为外键。所以我正在寻找一个查询,它只能获取那些在用户 table 中找到的行业的大学。所以它应该只获取用户 table
我不知道如何在我的查询中加入用户 table 并获得结果。
这就是我的大学模态现在的样子
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',
));
}
}
您可以在查找查询中加入数据。
$this->find('all', array(
'order' => 'uni_id',
'joins' => array(
array(
'table' => 'users ',
'alias' => 'UserJoin',
'type' => 'inner',
'conditions' => array(
'UserJoin.uni_id = University.id'
)
),
));
这里是official doc。希望对您有所帮助