Take the 2-minute tour ×

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

share|improve this question
You have asked the same question 3 times in as many hours. –  Mike B Mar 2 '09 at 14:22
add comment

1 Answer

up vote 2 down vote accepted


Please resist posting multiple times so quickly.

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

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
share|improve this answer
add comment

Your Answer


