S
S
Snewer2016-06-06 16:19:37
Yii
Snewer, 2016-06-06 16:19:37

How to get joined table in ActiveRecord?

Hello!
There is some ActiveRecord model Admin, which represents a table of the following structure:
id
user_id
admin_value
There is also an ActiveRecord model User, which represents a table:
id
username
email
Is it possible to
get data in the form (in In a normal query, I would use JOIN and get the required array at the output):
user_id
username
email
admin_value
i.e. you need one model to display the properties of both models at once

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Bukharev, 2016-06-06
@Snewer

You need to read the documentation on the topic of relationships in Yii, the essence of your question is just in relationships

J
jus0807, 2016-06-06
@jus0807

You can get data from the user model in the admin model either through a link, as you correctly wrote $admin->user->username
, or there is a second option - select the data as an array, but then auto-completion in the IDE will not work.
It will look something like this Admin::find()->with('user')->select(['admin.*', 'user.username'])->asArray()->all();
at the output, you will get arrays of values ​​of all fields from the admin table and associated username values. Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question