D
D
Dilik Pulatov2017-04-19 12:24:52
Yii
Dilik Pulatov, 2017-04-19 12:24:52

How to use JOIN in Yii2?

Hello! I'm using JOIN for the first time, so I'll do something wrong... help me solve the problem...
so I wrote this query

Category::find()->leftJoin('post', 'post.category_id = category.id')->all()

there is no error, but when outputting to print_r(), there are no posts.... or I will do something wrong
. ..
I don't want to use with()
if it's not difficult to explain in detail with an example)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-19
@dilikpulatov

Category::find()->joinWith('post')->all()
and add a getPost relationship to the Category model

public function getPost(){
return $this->hasMany(Post::classname(),['category_id' => 'id']);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question