Yii2查询空间中的帖子
我有一个带有post表、内容表和空间表的DB 帖子是一种内容类型,空间是许多帖子的容器。我想得到一个空间内的所有职位 职位: 内容(对象标识-->post.id): 空间(id-->内容。空间\u id): 要在空间中获取帖子,控制器函数如下所示:Yii2查询空间中的帖子,yii2,yii2-model,Yii2,Yii2 Model,我有一个带有post表、内容表和空间表的DB 帖子是一种内容类型,空间是许多帖子的容器。我想得到一个空间内的所有职位 职位: 内容(对象标识-->post.id): 空间(id-->内容。空间\u id): 要在空间中获取帖子,控制器函数如下所示: $posts = Post::find() ->joinWith('content', false) ->where(['{{content}}.space_id' => $space_id]) ->all();
$posts = Post::find()
->joinWith('content', false)
->where(['{{content}}.space_id' => $space_id])
->all();
public function getItems()
{
return $this->hasMany(Content::className(), ['id' => 'pk'])
->viaTable('contentcontainer', ['class' => 'space_id']);
}
Post模型具有以下功能,用于获取Post的内容对象:
public function getContent() {
return $this->hasOne(Content::className(), ['object_id' => 'id'])->andOnCondition(['{{content}}.object_model' => 'humhub\modules\post\models\Post']);
}
在数据库模式发生更改之前,这种方法一直工作得很好
现在内容表中不再有space\u id
列。相反,有一个新的表contentcontainer
,其中包含一个pk
字段,该字段替换空格\u id,还有一个class
字段(即space
class),用于标识pk是用于空格的(表中还有一个class
)
表/关系现在是:
邮政表格:
id object_id
--------------
1 22
内容表(对象标识-->post.id):
Contentcontainer表(id-->content.Contentcontainer\u id)
空间(id-->contentcontainer):
为了获得空间中的所有帖子,我现在必须链接3个表:post、content和contentcontainer
我是否将contentcontainer关系添加到Post模型?或者在发布模型中修改内容模型关系?不知道如何最好地处理而不写一个大的草率的查询
我将控制器功能更改为:
$posts = Post::find()
->where(['{{contentcontainer}}.pk' => $space_id])
->andWhere(['{{contentcontainer}}.class' => 'humhub\modules\space\models\Space'])
不确定这是否正确,我在
Post
模型中设置contentcontainer
关系时遇到了麻烦。似乎您有一个连接表-contentcontainer
。官方Yii2文件中有一个例子
在您的案例中,Post
模型中的关系可能是这样的:
$posts = Post::find()
->joinWith('content', false)
->where(['{{content}}.space_id' => $space_id])
->all();
public function getItems()
{
return $this->hasMany(Content::className(), ['id' => 'pk'])
->viaTable('contentcontainer', ['class' => 'space_id']);
}
现在,您的控制器函数将获得
$posts
在一个连接中执行两个连接。在空间中创建此方法
模型:
public function getPosts() {
return Post::find()
->innerJoin('contentcontainer', ['and',
'contentcontainer.pk = space.id',
'contentcontainer.class = "humhub\modules\space\models\Space"'])
->innerJoin('content', 'content.contentcontainer_id = contentcontainer.id')
->innerJoin('post', 'post.object_id = content.id');
}
下面是我解决这个问题的方法(结果来自内容模型,而不是发布模型):
请把3的结构贴出来tables@gmc添加了表结构
id
--------------
3
$posts = Post::find()
->where(['{{contentcontainer}}.pk' => $space_id])
->andWhere(['{{contentcontainer}}.class' => 'humhub\modules\space\models\Space'])
public function getItems()
{
return $this->hasMany(Content::className(), ['id' => 'pk'])
->viaTable('contentcontainer', ['class' => 'space_id']);
}
public function getPosts() {
return Post::find()
->innerJoin('contentcontainer', ['and',
'contentcontainer.pk = space.id',
'contentcontainer.class = "humhub\modules\space\models\Space"'])
->innerJoin('content', 'content.contentcontainer_id = contentcontainer.id')
->innerJoin('post', 'post.object_id = content.id');
}
$content = Content::find()
->joinWith('contentContainer')
->andWhere(['{{contentcontainer}}.pk' => $space_id])
->andWhere(['{{contentcontainer}}.class' => 'humhub\modules\space\models\Space'])
->andWhere(['{{content}}.object_model' => 'humhub\modules\post\models\Post'])
->asArray()
->all();