I have the following working find() function in Yii2:
User::find()->select('id, name')->where(['status' => '10'])->all()
However, the User model also has the following attributes:
'credit'
'ammount'
I need to check if the 'ammount' minus 'credit' is more that a value ($price) I am passing to the function.
How can I make a User:find() query where I am only getting the user objects where the amount minus credit is larger than the value I am checking against?
Thanks
答案 0 :(得分:1)
我假设金额和积分都在user
表中,因此您可以执行类似的操作。
User::find()->select([new \yii\db\Expression('id,amount,credit')])
->where(['status' => '10'])
->andWhere('amount - credit > :yourAmount',[':yourAmount'=>$price])
->all();
或使用addParams()
绑定自定义值
User::find()->select([new \yii\db\Expression('id,amount,credit')])
->where(['status' => '10'])
->andWhere('amount - credit > :yourAmount')
->addParams([':yourAmount'=>$price])
->all();