How to use OR and AND operator in active record

Below is what i want to achieve in plain SQL:

SELECT * from user WHERE status = self::STATUS_ACTIVE AND email = $email OR phone = $phone;

I tried the code below in active record and i keep getting unknown column:

return User::find()
->where([‘and’,[‘status’ => self::STATUS_ACTIVE],[‘or’,‘email’ => $email,‘phone’ => $phone]])
->one();

@softark please do you mind taking a look at this?

Hi @chinaka,

You need to enclose the hash-format conditions in brackets.

return User::find()
    ->where([
        ‘and’,
        [‘status’ => self::STATUS_ACTIVE],
        // [‘or’, ‘email’ => $email, ‘phone’ => $phone]
        [‘or’, [‘email’ => $email], [‘phone’ => $phone]]
    ])->one();
1 Like