我正在尝试使用Active Record在Yii中建立MANY_MANY关系.
我有三张桌子
轮廓
profile_id
profile_description
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,
),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。