Yii2 select by max date?

Your query is the equivalent of:

SELECT * FROM A ORDER BY created_date DESC LIMIT 1;

You can order your records by created_date in descending order and get the first record i.e:

$model = A::find()->orderBy('created_date DESC')->limit(1)->one();

Why limit(1)? As pointed out by nicolascolman, according to the official Yii documentation:

Neither yii\db\ActiveRecord::findOne() nor yii\db\ActiveQuery::one() will add LIMIT 1 to the generated SQL statement. If your query may return many rows of data, you should call limit(1) explicitly to improve the performance, e.g., Customer::find()->limit(1)->one().


$maxdate=A::find()->max('created_date');