2011-10-23 14 views
3

アクティブレコードを使用してYiiでMANY_MANY関係を設定しようとしています。Yii MANY_MANY関係を設定する

私は3つのテーブル

プロファイル を持って説明profile_id profile_description

カテゴリ CATEGORY_ID カテゴリ名

profile_category は

私のモデルは、プロフィールのカテゴリ、およびProfileCategoryある CATEGORY_IDを説明profile_id。

私は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です。

Categoryモデル

'プロフィール'、「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モデル

'カテゴリー'、 '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); 
を実行するました
関連する問題