Last Entry

Hi,

I’m trying to get the last entry of a driver and I’m doing this:




$driverLocs = DriverStats::find()->where(['driver_id' => $driverid])

				->orderBy(['driver_stats_id' => SORT_DESC])

				->asArray()

				->one();




but it is doing this:

SELECT * FROM tbl_driver_stats WHERE driver_id=‘70’ ORDER BY driver_stats_id DESC

I don’t want to select * because it’s taking forever to do that since I have 12 million records. How can I get just the last entry of that specific driver ID?

I thing you want the "limit 1" statement


Customer::find()->limit(1)->one()



I did not test this one:


$driverLocs = DriverStats::find()->where(['driver_id' => $driverid])

                                ->limit(1)

                                ->orderBy(['driver_stats_id' => SORT_DESC])

                                ->asArray()

                                ->one();

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html

That worked. Thank you so much!