lilbiscuit lilbiscuit - 1 year ago 238
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?

Answer Source

findOne will not directly handle this.

You can do it as follows:

$customer = Customer::find()->andWhere(['>', 'age', 30])->andWhere(['status' => 1])->one();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download