Answer the question
In order to leave comments, you need to log in
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
You need to read the documentation on the topic of relationships in Yii, the essence of your question is just in relationships
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 questionAsk a Question
731 491 924 answers to any question