yii2活动记录查找列不相等 [英] yii2 active record find column not equal

查看:38
本文介绍了yii2活动记录查找列不相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码可以从数据库中查找状态为活动且角色为用户的用户

I have this code to find a user from db which status is active and role is user

public static function findByUsername($username)
{
 return static::find(['username' => $username, 'status' => static::STATUS_ACTIVE, 'role'=>'user']);
}

我需要找到一个角色不等于用户"的用户.我该怎么做?

I need to find a user that role is not equal to "user". How can I do this?

P.S:我使用的是 YII2

P.S: I'm using YII2

推荐答案

我想提供另一种解决方案,它对我来说更优雅.当我需要检查不相等操作时,我通常从 Yii 1 开始使用这种方法.

I want to offer another solution, it's more elegant for me. I usually use this approach since Yii 1 when i need check not equal operation.

$models = Book::find()->where('id != :id and type != :type', ['id'=>1, 'type'=>1])->all();

这篇关于yii2活动记录查找列不相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆