B
B
Brons2017-05-29 17:42:05
MySQL
Brons, 2017-05-29 17:42:05

How to sort by related field in yii2?

I am implementing a chat in which the user writes questions, and the company employee answers. I have a Users
table

  • id
  • name
  • ......

and a table with
Messages
  • id (int)
  • user(int)
  • text (text)
  • answer(int)

In the Messages table, the user id is indicated in the user field, and in the answer field:
0- if the message was written by the user (question)
1- if the message was written by an employee of the company (answer).
How to sort users so that the first are users whose answer field in the last message is 0, that is, those who wrote a question but did not receive an answer go first.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2017-05-29
@AloneCoder

Add a hasUnansweredQuestions field to users

M
Maxim Timofeev, 2017-05-30
@webinar

Well, just sorting by this field->sort('answer ASC')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question