2

Saying I want to convert the following SQL query into ElementCriteriaModel:

SELECT * FROM users WHERE fieldA = 1 AND (fieldB = 1 or fieldC = 1)

I can't find a way in ElementCriteriaModel to do so.

{% craft.users({
    fieldA: 1,
    fieldB: 1,
    fieldC: 1, // where can we put the 'or' filter?
}) %}

I can do this using DbCommand, but just curious if there's anyway we can do that via ElementCriteriaModel?

1

I suspect you're looking for this section on passing parameters to ElementCriteriaModels, in the docs.

The examples fail to highlight one aspect of the query you're building, but I think you'll find the same strategies hold up for many field values, as well as basic Element properties (like id, title, etc.)

For example…

craft.users({ myFieldA: [1, 2, 3] }).all()

…will fetch User Elements with the custom myFieldA matching the values 1, 2, or 3. The or is implicit!

1
  • Hey man, thanks for your reply. However, that's not exactly what I'm looking for. Your answer is doing an 'OR' filter on ONE field, but I'm trying to do an 'OR' on two different fields and also combine with 'AND' filter with other fields.
    – Wei
    Dec 12 '18 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.