【问题标题】:Setting Up Yii MANY_MANY Relationship设置 Yii MANY_MANY 关系
【发布时间】:2011-10-23 19:14:05
【问题描述】:

我正在尝试使用 Active Record 在 Yii 中建立一个 MANY_MANY 关系。

我有三张桌子

个人资料 profile_id profile_description

类别 类别ID 类别名称

profile_category profile_id category_id

我的模型是 Profile、Category 和 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 relations()
{
    return array(
        'category'=>array(
            self::BELONGS_TO,
            'Category',
            'category_id',
        ),
        'profile'=>array(
            self::BELONGS_TO,
            'Profile',
            'profile_id',
        ),
    );
}

控制器

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>

有什么想法吗?谢谢你!视图中没有显示任何内容。

【问题讨论】:

    标签: php mysql activerecord yii


    【解决方案1】:

    您必须在配置文件模型中设置一个名为 profileCategory 的属性(而不是 profile_category):

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

    您可以将它与 and 条件一起使用,如下所示:

           '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'
              ),
          );
      }
      

      如果数据库的模型有
      很多 self::BELONGS_TO 并且数据库只有 PK (PrimaryKeys)

      这样的代码会被正确执行

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多