Yii JOINs

I have three tables.

1: users

2: videos

3: video_favorites

videos is related to users (by user_id).

video_favorites is related to videos (by video_id) and [b]users /b.

user has data about user (first_name, last_name, etc)

videos has metadata about the video (title, url, description, etc).

video_favorites has two relations (user_id and video_id).

I would like to make JOIN of three tables - users, video and video_favorites. However, I only need to have information about user and video and then display it as list of the videos with pagination.

I do not know how to make this JOIN.

Btw, for each table I have ActiveRecord class, which means that I would like to make it in the ActiveRecord’s style.

I need something like the following:




$videos = VideoFavorites::find()

    ->select('video_favorites.*')

    ->leftJoin('videos', '`videos`.`id` = `video_favorites`.`video_id`')

    ->all();



but this does not work.

I tried another way, which is defining hasOne() or hasMany() relation in the model and then using it like the following:




$query = VideoFavorites::find()

            ->joinWith('video');



Did not solve the problem. It is just displaying the data from video_favorites.

Means you need to have Video model and start your query with it.

Means you need User model and need it as a relation to Video.

video_favorites seems to be just interconnecting table so overall you can define a relation in the Video model connecting User via video_favorites table.

Then querying is simple:




$videos = Video::find()->joinWith('users')->all();