繁体   English   中英

设置Yii MANY_MANY关系

[英]Setting Up Yii MANY_MANY Relationship

我正在尝试使用Active Record在Yii中设置MANY_MANY关系。

我有三张桌子

profile profile_id profile_description

category category_id category_name

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>

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

逻辑id首先建模关系会更好...

这里有一个例子。

分类模型

, ...)' ' profile ','profile_category( ,...)'

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

档案模型

, ...)' ' category ','profile_category( ,...)'

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

它的实际数据库模型有
很多 自我:: BELONGS_TO和数据库只有PK(PrimaryKeys)

这样的代码将被正确执行

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

您必须在配置文件模型中设置名为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,
        ),

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM