Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to build a complex criteria in "Model::find()" conditions?

For example a "user" has many property, like username, email, nick, active, privilege and so on. And I want to filter the records with a keyword and active status (only two vaule, YES and NO).

If keyword is not null, it should filter the records with username like '%keyword%' or email like %keyword%, if it's null, it should show all the records with other conditions.

If active = 'YES' , it should filter the records with that conditions....

How to build this conditons??

edited Oct '17
    'conditions'=>'active = :active: AND (:keyword: IS NOT NULL AND (username LIKE :keyword: OR email LIKE :keyword:)) OR (:keyword: IS NULL AND 1=1)',
    'bind'=>['keyword'=>$keyword, 'active'=>'YES']

Or use the modelsManager's createQuery or createBuilder


Thanks a lot!