A
A
alex_belouss2017-10-05 09:48:22
Yii
alex_belouss, 2017-10-05 09:48:22

How to get last 10 records in active record?

$latests = Project::find()->where(['status' => 1])->limit(10)->all();
I do this, but it pulls out from the end of the base, that is, the first 10, but the last 10 are needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-10-05
@qonand

all data in the database is stored in an unordered form, so what is the "last 10" is known only to you and God. To solve the problem, you need to determine by what attribute the latest records are determined and sort the data accordingly, according to this attribute, for example:

$latests = Project::find()->where(['status' => 1])->orderBy(['id' => SORT_DESC])->limit(10)->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question