YII2 的复杂查询的一个例子
例如需要查询的SQL是
select * from user where (status=10) and ( username like %aaa% or email like %aaa% or mobile like %aaa%);
也就是第一个参数是必要条件, 后面的是模糊查询, 那么写法是:
$param = 'aaa';
$query = User::find()->where(['status'=>10,])->andwhere([ 'or', ['like' , 'username', $param], ['like' , 'email', $param],
['like' , 'mobile', $param],
])->all();
如果用原生态的SQL查询是:
$param = 'aaa';
$query = (new \yii\db\Query()) ->from('user') ->where("status=:status and (username like :param or `name` like :param or `mobil` like :param)") ->addParams([':status'=>10,':param'=>'%$param%']) ->all();