lilbiscuit lilbiscuit - 3 months ago 5x
SQL Question

Using relational expression in yii findOne()

In Yii2, to use findOne() to grab a single record with exact match is simple. For example:

// find the first customer whose age is 30 and whose status is 1
$customer = Customer::findOne(['age' => 30, 'status' => 1]);

But what if you want to find one where
is less than 30?


findOne will not directly handle this.

You can do it as follows:

$customer = Customer::find()->andWhere(['>', 'age', 30])->andWhere(['status' => 1])->one();