N
N
newbee2017-07-05 19:01:33
MySQL
newbee, 2017-07-05 19:01:33

Yii2 activeRecord - joins - how to correctly get the results of the selection?

Let's say there are 2 tables: user(id, name, pass), and post(id, text, user_id) associated with FOREIGN KEY in the Post model:

public function getUser(){
    return $this->hasOne(User::className(), ['id' => 'user_id']);
}

Further, somewhere we have a request for posts with a join of user-authors: Actually, the question is: How exactly to get the user id from the results of this sample, and how to get the id of the post? (table column name prefixes are not considered, otherwise this question simply would not be asked)
$query = Post::find()->joinWith('user');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-07-05
@newbee

Good evening.

$query->id; // id post
$query->user->id; // id user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question