M
M
Mark2017-10-03 10:24:09
MySQL
Mark, 2017-10-03 10:24:09

How to correctly organize the output of a field if it is in another table?

Hello.
There is a table of reviews, it contains, in addition to thematic fields (comment, rating), the user_id field. But on the front-end, the output of user_id will be inconvenient for users, but I would like to display the username.
But to get the username, each review needs +1 request to get user information by id. Thus, for 5 reviews + 5 requests. I don't think this is the best option. There is an idea - to store a copy of the username in the feedback table, but is this correct?
What is the correct option in this situation - requests, or storing a copy of the username? Or are there any other correct options?
Thanks for answers.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Wolf, 2017-10-03
@Wolfnsex

Or are there any other correct options?
LEFT JOIN ?

A
Anton, 2017-10-03
@karminski

What you are looking for is called relational data:
www.yiiframework.com/doc-2.0/guide-db-active-recor...

M
Maxim Fedorov, 2017-10-03
@qonand

as Anton said , first set up the relation. After that, just implement eager loading and regardless of the number of records, you will always have 1-2 queries

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question