Problem with getting data from tables in Yii2

42 Views Asked by At

I have created method in the Yii2 model Users to get all the replies for the current user

public function getAllRepliesForUsers() { return $this->hasMany(Replies::class, ['user_id' => 'id'])->viaTable('replies_links', ['replies_id' => 'id'])->where(['entity'=>'user']); } My replies tableenter image description here My users table enter image description here and the final table that links these two tables enter image description here

Is my method is correct?

1

There are 1 best solutions below

1
Lionel Bautista On

Here's the relationship of Users to the Replies. You can use the Model generator of Gii module so you won't get confused by manually typing them.

  public function getReplies()
{
    return $this->hasMany(Replies::className(), ['id' => 'reply_id'])->viaTable('rply_links', ['user_id' => 'id']);
}

(May I know what do you intend to do with the condition ->where(['entity'=>'user'])?).