Answer the question
In order to leave comments, you need to log in
Yii2 AR: Joining with Relations?
I don't quite understand the behavior of Active Record in Yii2 when we use the joinWith();
Code example method
// Relation is has many
return $this->hasMany(Cars::className(), ['user_id' => 'id']);
// Create and execute query to the db
User::find()->select(['user.*', 'cars.*'])->joinWith(['cars'])->where(['user.id' => $id])->one()
SELECT `user`.*, `cars`.* FROM `user` LEFT JOIN `cars` ON `user`.`id` = `cars`.`user_id` WHERE `user`.`id`='54'
SELECT * FROM `cars` WHERE `user_id`=54
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question