2011-10-23 55 views
3

我想在使用活動記錄的Yii中設置MANY_MANY關係。設置Yii MANY_MANY關係

我有三個表

輪廓 PROFILE_ID profile_description

類別 CATEGORY_ID CATEGORY_NAME

profile_category PROFILE_ID CATEGORY_ID

我的模型檔案,類別和ProfileCategory。

我想運行一個查詢使用category_id將拉起所有屬於該類別的配置文件。

這是類別模型中的信息。

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
     'profiles'=>array(
      self::MANY_MANY, 
      'Profile', 
      'profile_category(category_id, profile_id)', 
     ), 
     'profile_category'=>array(
      self::HAS_MANY, 
      'ProfileCategory', 
      'category_id', 
     ), 
    ); 
} 

剖面模型

public function relations() 
{ 
    // NOTE: you may need to adjust the relation name and the related 
    // class name for the relations automatically generated below. 
    return array(
    'categories'=>array(
      self::MANY_MANY, 
      'Category', 
      'profile_category(profile_id, category_id)' 
     ), 
     'profileCategory'=>array(
      self::HAS_MANY, 
      'ProfileCategory', 
      'profile_id' 
     ), 
    ); 
} 

ProfileCategory型號

​​

控制器

public function actionResults() 
{ 
    $category=$_POST['terms']; 
    $dataProvider=new CActiveDataProvider(
     'Profile', 
     array(
      'criteria'=>array(
       'with'=>array('profile_category'), 
       'condition'=>'display=10 AND profile_category.category_id=1', 
       'order'=>'t.id DESC', 
       'together'=>true, 
      ), 
     ) 
    ); 
    $this->render('results',array(
     'dataProvider'=>$dataProvider, 
    )); 
} 

查看

<div id=resultsleft> 
<?php 
foreach($dataProvider as $value) 
{ 
echo $value->profile_id; 
} 
?> 
</div> 

有什麼想法?謝謝!視圖中沒有顯示任何內容。

回答

2

你必須建立一個名爲profileCategory(不profile_category)在配置模型屬性:

'profileCategory'=>array(
     self::HAS_MANY, 
     'ProfileCategory', 
     'profile_id' 
    ), 

您可以用條件中使用它,如下所示:

 'criteria'=>array(
      'with'=>array('profileCategory'), 
      'condition'=>'display=10 AND profileCategory.category_id=1', 
      'order'=>'t.id DESC', 
      'together'=>true, 
     ), 
2

邏輯id先建立關係模型會更好...

有一個示例here

分類模型

'檔案',「profile_category( PROFILE_ID,...)」

public function relations() 
{ 
    return array(
     'profiles'=>array(
      self::MANY_MANY, 
      'Profile', 
      'profile_category(profile_id, category_id)' 
     ), 
     'profile_category'=>array(
      self::HAS_MANY, 
      'ProfileCategory', 
      'category_id', 
     ), 
    ); 
} 

剖面模型

'類別', 'profile_category( CATEGORY_ID,...)'

public function relations() 
{ 
    return array(
     'categories'=>array(
      self::MANY_MANY, 
      'Category', 
      'profile_category(category_id, profile_id)' 
     ), 
     'profile_category'=>array(
      self::HAS_MANY, 
      'ProfileCategory', 
      'profile_id' 
     ), 
    ); 
} 

其actualy如果數據庫的模型有
許多自:: belongs_to的和數據庫只有PK(PrimaryKeys)

這樣的代碼將被正確執行

print_r(Profile::model()->findByPk(5)->categories); 
print_r(Category::model()->findByPk(8)->profiles);