我在 Yii2 中有以下工作 find(( 函数:
User::find()->select('id, name')->where(['status' => '10'])->all()
但是,用户模型还具有以下属性:
'credit'
'ammount'
我需要检查"ammount"减去"信用"是否比我传递给函数的值($price(更多。
如何进行 User:find(( 查询,其中我只获取金额减去信用大于我正在检查的值的用户对象?
谢谢
我假设金额和信用在user
表中,因此您可以执行类似操作。
User::find()->select([new yiidbExpression('id,amount,credit')])
->where(['status' => '10'])
->andWhere('amount - credit > :yourAmount',[':yourAmount'=>$price])
->all();
或使用addParams()
绑定自定义值
User::find()->select([new yiidbExpression('id,amount,credit')])
->where(['status' => '10'])
->andWhere('amount - credit > :yourAmount')
->addParams([':yourAmount'=>$price])
->all();