M
M
Mors Clamor2019-11-01 23:38:35
Yii
Mors Clamor, 2019-11-01 23:38:35

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']);
    }

and post likes
public function getPost() {
      return $this->hasOne(Posts::className(), ['post_id' => 'like_id']);
    }

Posts with User are fine, but Posts->likes stubbornly returns emptiness. What's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-11-01
@66demon666

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 question

Ask a Question

731 491 924 answers to any question