I had a DB with a post table, a content table, and a space table.
A post is a type of content, and a space is a container for many posts. I want to get all posts within a space.
Post:
id object_id
--------------
1 22
Content (object_id --> post.id):
id space_id
------------------------
22 3
Space (id --> content.space_id):
id
--------------
3
To get posts within a space, the controller function looked like this:
$posts = Post::find()
->joinWith('content', false)
->where(['{{content}}.space_id' => $space_id])
->all();
And the Post model had this function to get the content object for a post:
public function getContent() {
return $this->hasOne(Content::className(), ['object_id' => 'id'])->andOnCondition(['{{content}}.object_model' => 'humhub\modules\post\models\Post']);
}
This worked perfectly until the database schema changed.
Now there is no longer a space_id
column in the content table. Instead, there is a new table contentcontainer
with a pk
field which replaces space_id, and a class
field (i.e. space
class) to identify that the PK is for a space( there is also a class
in the table).
The tables/relationships are now:
Post Table:
id object_id
--------------
1 22
Content table (object_id --> post.id):
id contentcontainer_id
------------------------
22 5
Contentcontainer Table (id --> content.contentcontainer_id)
id pk class
---------------
5 3 //Space
Space (id --> contentcontainer):
id
--------------
3
To get all posts within a space, I now have to link 3 tables: post,content,contentcontainer.
Do I add the contentcontainer relationship to the Post model? Or modify the content model relationship in the Post model? Not sure how best to tackle without writing a big sloppy query.
I changed the controller function to this:
$posts = Post::find()
->where(['{{contentcontainer}}.pk' => $space_id])
->andWhere(['{{contentcontainer}}.class' => 'humhub\modules\space\models\Space'])
Not sure this is right and I am stuck setting up the contentcontainer
relationship in the Post
model.