Skip to content

Commit bc2b165

Browse files
committed
Mention lack of CAST in direct where() use
craftcms/cms#16944
1 parent efd3792 commit bc2b165

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

docs/5.x/development/element-queries.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,16 +800,18 @@ You may call <craft5:craft\db\Query::asArray()> to skip populating element model
800800

801801
### Content and Custom Fields
802802

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+
803805
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.
804806

805-
Craft also tries to intercept direct use of `.where()` and `.orderBy()`, such that field instance handles work naturally. <Since ver="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. <Since ver="5.6.0" feature="Field instance handle mappings for query constraints and ordering" />
806808

807809
```twig
808810
{% set safeStews = craft.entries()
809811
.section('dishes')
810812
.andWhere([
811-
['>', 'scovilleRating', 1000],
812-
['<', 'scovilleRating', 9000],
813+
['>', 'scovilleRating', '1000'],
814+
['<', 'scovilleRating', '9000'],
813815
])
814816
.andWhere([
815817
'not',
@@ -821,7 +823,11 @@ Craft also tries to intercept direct use of `.where()` and `.orderBy()`, such th
821823
.all() %}
822824
```
823825

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):
825831

826832
::: code
827833
```twig{1,7} Twig

0 commit comments

Comments
 (0)