How do I query a table based with HABTM relationship while providing additional conditions in CakePHP?

前端 未结 1 789
Happy的楠姐
Happy的楠姐 2020-12-12 05:08

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 tabl

1条回答
  •  佛祖请我去吃肉
    2020-12-12 05:30

    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 讨论(0)
提交回复
热议问题