在CakePHP中提供其他条件时,如何查询基于HABTM关系的表? [英] How do I query a table based with HABTM relationship while providing additional conditions in CakePHP?

查看:152
本文介绍了在CakePHP中提供其他条件时,如何查询基于HABTM关系的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在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关系的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆