Skip to content

Commit cc29ae0

Browse files
authored
Merge pull request #11891 from mpdude/expression-matching-caveats
Add more detailed caveats for using the Collection filtering API
2 parents bd4a053 + c259371 commit cc29ae0

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

docs/en/reference/working-with-associations.rst

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,35 @@ methods:
736736

737737
.. note::
738738

739-
There is a limitation on the compatibility of Criteria comparisons.
740-
You have to use scalar values only as the value in a comparison or
741-
the behaviour between different backends is not the same.
739+
Depending on whether the collection has already been loaded from the
740+
database or not, criteria matching may happen at the database/SQL level
741+
or on objects in memory. This may lead to different results and come
742+
surprising, for example when a code change in one place leads to a collection
743+
becoming initialized and, as a side effect, returning a different result
744+
or even breaking a ``matching()`` call somewhere else. Also, collection
745+
initialization state in practical use cases may differ from the one covered
746+
in unit tests.
747+
748+
Database level comparisons are based on scalar representations of the values
749+
stored in entity properties. The field names passed to expressions correspond
750+
to property names. Comparison and sorting may be affected by
751+
database-specific behavior. For example, MySQL enum types sort by index position,
752+
not lexicographically by value.
753+
754+
In-memory handling is based on the ``Selectable`` API of `Doctrine Collections <https://www.doctrine-project.org/projects/doctrine-collections/en/stable/index.html#matching>`.
755+
In this case, field names passed to expressions are being used to derive accessor
756+
method names. Strict type comparisons are used for equal and not-equal checks,
757+
and generally PHP language rules are being used for other comparison operators
758+
or sorting.
759+
760+
As a general guidance, for consistent results use the Criteria API with scalar
761+
values only. Note that ``DateTime`` and ``DateTimeImmutable`` are two predominant
762+
examples of value objects that are *not* scalars.
763+
764+
Refrain from using special database-level column types or custom Doctrine Types
765+
that may lead to database-specific comparison or sorting rules being applied, or
766+
to database-level values being different from object field values.
767+
768+
Provide accessor methods for all entity fields used in criteria expressions,
769+
and implement those methods in a way that their return value is the
770+
same as the database-level value.

0 commit comments

Comments
 (0)