M
M
Maxim2018-05-27 23:30:32
Yii
Maxim, 2018-05-27 23:30:32

Yii how to do reverse sorting in hasMany relation?

There is the following connection:

/**
     * @return \yii\db\ActiveQuery
     */
    public function getMessages()
    {
        return $this->hasMany(Message::className(), ['dialog_id' => 'id']);
    }

I need the array to be sorted by the "id" or "created_at" field with the DESC parameter (reverse sort). The link array should output the most recent data first, and then the older ones

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-05-27
@myks92

Good evening.

public function getMessages()
    {
        return $this->hasMany(Message::className(), ['dialog_id' => 'id'])->orderBy(['id' => SORT_DESC]);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question