Answer the question
In order to leave comments, you need to log in
Yii2 why can't get related data?
Hello! For some reason, I can't get the associated data. There are 2 Post models
:
....
public function getUser() {
return $this->hasOne(User::className(), ['id' => 'author_id']);
}
public function getLikes() {
return $this->hasMany(PostLikes::className(), ['like_id' => 'post_id']);
}
public function getPost() {
return $this->hasOne(Posts::className(), ['post_id' => 'like_id']);
}
Answer the question
In order to leave comments, you need to log in
How do you know what's wrong? Where is the structure of tables or entities? Maybe you configured the connection incorrectly? Maybe there is no such ID!
Most likely so. Swap the IDs, but without the structure, you can't say:
public function getLikes() {
return $this->hasMany(PostLikes::className(), ['post_id' => 'like_id']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question