Monday, April 11, 2011

How can i implement this in cakephp

How can i do this in CAKEPHP: I,m using two tables (regions and safaris) having 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

From stackoverflow
  • juma,

    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
       }
    

0 comments:

Post a Comment