我有具有关系的图像模型:
public function getAlbums()
{
return $this->hasMany(ImagesTerms::className(), ['id' => 'term_id'])
->viaTable(ImagesTermsRelations::tableName(), ['object_id' => 'id'])
->andWhere(['type'=>'album']);
}
在图像查看页面上,我想显示具有相同相册的随机图像。任何人都可以帮忙吗?
我尝试了以下查询,但它没有给我想要的东西:
$related = Images::find()->with([
'albums' => function($query) {
$query->andWhere(['in', 'id', [2]]);
}
])->where(['status'=>'1'])->orderBy('rand()')->limit(9)->all();
此查询不包括其他相册,但不包括图像。显示具有其他专辑的图像,但没有专辑标签。