PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-设置Yii MANY_MANY关系大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

我有三张桌子

轮廓
profile_id
profile_description

类别
category_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>

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

解决方法:

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

大佬总结

以上是大佬教程为你收集整理的php-设置Yii MANY_MANY关系全部内容,希望文章能够帮你解决php-设置Yii MANY_MANY关系所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: