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

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

问题描述

我如何在 CakePHP 中执行此操作:我正在使用两个具有多对多关系的表(regionssafaris).这些表由另一个名为 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,

请不要这么快多次发帖.

Please resist posting multiple times so quickly.

如需更多参考,请参阅 Cakebook Has-And-Belongs-To-Many 关系.这正是你所描述的.参见 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 模型(在 region、safari 和 region_safaris 之间建立了 HABTM 关系;检查模型中 var $hasAndBelongsToMany 数组上的joinTable"索引),您将执行此操作:

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天全站免登陆