2013-08-12 26 views
0

我有表 1)tours(id,title) 2)categories(id,title) 3)tours_categories tour_id,CATEGORY_ID)黑客中的主題搜索MANY_MANY在YII中,但到處有很多信息,所有不同,幫助我瞭解

型號之旅:

public function relations() 
{ 
    return array ( 
          'Category' => array (self :: MANY_MANY, 
           'Categories', 
           'tours_categories (tour_id, category_id)' 
     ), 
    ); 
} 

模型類別:

public function relations() 
{ 
    return array ( 
        'Tours' => array (self :: MANY_MANY, 
         'Tours', 
         'tours_categories (category_id, tour_id)' 
     ), 
    ); 
} 

問題:

我想搜索的數據庫表tours_categories和選擇所有旅行團=一個類別ID ......怎麼辦呢正確

在控制器ToursController我想要做這樣的事情

$tour = Tours::model()->with ('category')->findAllByAttributes (array ('category.id' => $id)); 

但它不是當然的作品。怎麼做?

回答

1
$tours = Tours::model()->with(array(
      'category'=>array(
       'alias' => 'ct', //to avoid error ambiguous column category when implementing query 
       'condition'=>'ct.category_id = :cid', 
       'params'=>array(':cid'=>$id 
       ))) 
     )->findAll(); // you could put more condition for findAll, it returns array of Tour-s after filtered. 

順便說一句,一個次要的事情,對模型旅遊的關係MANY_MANY名稱category不諧調感。它應改爲categories。一旦你更新它,你也應該更新上面的原因查詢。

相關問題