S
S
Stanislav2016-08-04 18:14:14
MySQL
Stanislav, 2016-08-04 18:14:14

How should I access 2 tables by JOIN in yii2?

There is a request:

SELECT `users`.`userName`,`posts`.`title`,`posts`.`text`
FROM `posts`
  JOIN `users`
    ON `users`.`id`=`posts`.`user_id`

Those. we get a table in which all user IDs are replaced by their nicknames. How to implement this using ActiveRecord?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stanislav, 2016-08-05
@Stasgar

Problem solved by dependencies.

M
Michael, 2016-08-04
@springimport

Here is an example .

A
Andrew, 2016-08-04
@mhthnz

$posts = Posts::find()->leftJoin('users', ['users.id' => 'posts.user_id'])->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question