Yii2深度加入

时间:2016-02-22 21:42:08

标签: join yii2

我正在使用yii2。我有四张桌子:

  1. posts:包含用户发布的帖子(post_iduser_idpost
  2. mapPostClasses:使用他们的课程发布帖子(map_idpost_idclass_id
  3. mapUsersClasses:使用类(map_iduser_idclass_id
  4. 映射用户
  5. Users:用户详细信息表(user_idname等。)
  6. 现在我想要Posts table的加入查询来查找所有帖子WHOSE CLASS_ID IS SAME AS THE GIVEN USER'S CLASS_ID

    我目前使用的示例SQL是

    Select p.* FROM posts p ,mapUsersClass muc, mapPostsClass mpc
    muc.user_id = $user_id
    AND mpc.class_id = muc.class_id
    AND p.post_id = mpc.post_id 
    

1 个答案:

答案 0 :(得分:0)

您可以使用ActiveQuery

$query = (new \yii\db\Query())
->select('*')
->from('posts p')
->join('join','mapPostsClass mpc','p.post_id = mpc.post_id')
->join('join','mapUsersClass muc','mpc.class_id = muc.class_id')
->where('muc.user_id = '.$user_id.'')
->all();