Skip to content

Commit 4372b03

Browse files
committed
Document changes in cakephp/cakephp#17873
1 parent 504af6a commit 4372b03

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

en/appendices/5-1-migration-guide.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ I18n
131131
a typesafe way to cast request data or other input and return ``null`` when
132132
conversion fails.
133133

134+
ORM
135+
---
136+
137+
- Setting the ``preserveKeys`` option on association finder queries. This can be
138+
used with ``formatResults()`` to replace association finder results with an
139+
associative array.
140+
134141
TestSuite
135142
---------
136143

en/orm/query-builder.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,28 @@ As seen above, the formatters attached to associated query builders are scoped
707707
to operate only on the data in the association. CakePHP will ensure that
708708
computed values are inserted into the correct entity.
709709

710+
If you want to replace the results of an association finder with
711+
``formatResults`` and your replacement data is an associative array, use
712+
``preserveKeys`` to retain keys when results are mapped to the parent query. For
713+
example::
714+
715+
public function findSlugged(SelectQuery $query): SelectQuery
716+
{
717+
return $query->applyOptions(['preserveKeys' => true])
718+
->formatResults(function ($results) {
719+
return $results->indexBy(function ($record) {
720+
return Text::slug($record->name);
721+
});
722+
});
723+
}
724+
725+
The ``preserveKeys`` option can be set as a contain option as well.
726+
727+
.. versionadded:: 5.1.0
728+
The ``preserveKeys`` option was added.
729+
730+
731+
710732
.. _advanced-query-conditions:
711733

712734
Advanced Conditions

0 commit comments

Comments
 (0)