问题描述
我如何在CakePHP中这样做:
我使用两个表( regions
和 safaris
)具有多对多关系。这些表由另一个名为 regions_safaris
的表连接(字段 region_id
& safari_id
)。我想获取所有 safaris
满足给定的标准,如5天的持续时间和在给定的地区。这应该在CakePHP中实施
How can i do this in CakePHP:I'm using two tables (regions
and safaris
) having a many-to-many relationship. The tables are joined by another table called regions_safaris
(with the fields region_id
& safari_id
). I want to fetch all safaris
meeting a given criteria like durations of 5 days and in a given region. This should be implemented in CakePHP
推荐答案
juma,
发布多次这么快。
有关更多参考,请参阅蛋糕书与属于多对的关系。这正是你的描述。请参阅 http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
For more reference, please see the Cakebook Has-And-Belongs-To-Many relationship. It is exactly what you're describing. See http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
简而言之,假设您已正确设置Cake模型(使用在regions,safaris和regions_safaris之间建立的HABTM关系;检查var上的'joinTable' $ hasAndBelongsToMany数组,在模型中),你将这样做:
In short, assuming you have the Cake models setup correctly (with the HABTM relationship established between regions, safaris, and regions_safaris; check the 'joinTable' index on the var $hasAndBelongsToMany array, in the models), you would do this:
class SafariModel extends AppModel
{
var $name = 'Safari';
var $hasAndBelongsToMany = array( 'Tag'=>array( ..., 'joinTable'=>'regions_safaris', ... );
function findSafaris( $duration = 5, $region = null )
{
return $this->find('all', array('conditions'=>array('Region.region_name'=>$region, 'Safari.duration'=>$duration) );
}
... // rest of class
}
这篇关于在CakePHP中提供其他条件时,如何查询基于HABTM关系的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!