You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/5.x/development/element-queries.md
+10-4Lines changed: 10 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -800,16 +800,18 @@ You may call <craft5:craft\db\Query::asArray()> to skip populating element model
800
800
801
801
### Content and Custom Fields
802
802
803
+
We typically recommend using the [methods corresponding to your fields’ global handles](#querying-with-custom-fields) to narrow element queries—but sometimes, criteria can only be expressed with direct use of ActiveRecord’s `where()` and `andWhere()`.
804
+
803
805
Most custom field values are stored in a single JSON column, keyed by their unique field instance UUID. Craft handles this automatically when using a field or field instance’s built-in query methods (i.e. `.myCustomDateField('<= 2025-11-05')`) by building the appropriate “JSON extraction” expression.
804
806
805
-
Craft also tries to intercept direct use of `.where()` and `.orderBy()`, such that field instance handles work naturally. <Sincever="5.6.0"feature="Field instance handle mappings for query constraints and ordering" />
807
+
Craft attempts to detect use of field instance handles in `.where()` and `.orderBy()`, and map them to the corresponding extraction expression. <Sincever="5.6.0"feature="Field instance handle mappings for query constraints and ordering" />
806
808
807
809
```twig
808
810
{% set safeStews = craft.entries()
809
811
.section('dishes')
810
812
.andWhere([
811
-
['>', 'scovilleRating', 1000],
812
-
['<', 'scovilleRating', 9000],
813
+
['>', 'scovilleRating', '1000'],
814
+
['<', 'scovilleRating', '9000'],
813
815
])
814
816
.andWhere([
815
817
'not',
@@ -821,7 +823,11 @@ Craft also tries to intercept direct use of `.where()` and `.orderBy()`, such th
821
823
.all() %}
822
824
```
823
825
824
-
However, Craft can’t _always_ infer what should be a plain column name or a field or field instance handle, when working with advanced [condition](#conditions) and [execution](#query-execution) methods. This means you are responsible for building equivalent field value expressions. Typically, this involves a field instance handle and a _field layout provider_ (like an entry type, asset volume, category group, or other component that manages a field layout):
826
+
::: warning
827
+
Note that our `scovilleRating` constraints (`'1000'` and `'9000'`) are passed as _strings_, not _integers_. This is necessary, as JSON values are not “cast” during comparison when using `where()`, directly.
828
+
:::
829
+
830
+
Craft can’t _always_ infer what should be a plain column name or a field or field instance handle, when working with advanced [condition](#conditions) and [execution](#query-execution) methods. This means you are responsible for building equivalent field value expressions. Typically, this involves a field instance handle and a _field layout provider_ (like an entry type, asset volume, category group, or other component that manages a field layout):
0 commit comments