Like in ActiveRecord to a joined table?

Hello!

There is some ActiveRecord model Admin, which is a table with the following structure:
id
user_id
admin_value

As there is a User ActiveRecord model that represents the table:
id
username
email

Is it possible when retrieving data from the first models: Admin::findOne()
to obtain data (in normal query I would use the JOIN and the output would get the required array):

user_id
username
email
admin_value

ie you need that one model is deduced properties of both models at once
July 8th 19 at 16:26
2 answers
July 8th 19 at 16:28
Solution
You need to read the documentation on the theme of relations in Yii, the essence of Your question just links
As I understand it, the associated data are in separate property, that is, for example, to get the user name need to do like this: $admin->user->username instead of $admin->username - Akeem_Erdman26 commented on July 8th 19 at 16:31
Yes, the user property will be a reference to the associated User model - adell17 commented on July 8th 19 at 16:34
July 8th 19 at 16:30
The data from the model user in model admin you can get a or through a link, as you have correctly written $admin->user->username
or is there a second option is to select the data as an array, but then the auto-completion in the IDE will not work.
It will look approximately so Admin::find()->with('user')->select(['admin.*', 'user.username'])->asArray()->all();
you'll get arrays of values from all fields from table admin and associated username. Something like that.

Find more questions by tags Yii