Skip to content

Commit d94b1bd

Browse files
authored
Add docs for CounterCacheBehavior::updateCounterCache(). (#7931)
Refs cakephp/cakephp#17967
1 parent c5dc9cb commit d94b1bd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Behavior Changes
1515
New Features
1616
============
1717

18+
ORM
19+
---
20+
21+
- ``CounterCacheBehavior::updateCounterCache()`` has been addded. This method
22+
allows you to update the counter cache values for all records of the configured
23+
associations.
24+
1825
Error
1926
-----
2027

en/orm/behaviors/counter-cache.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,28 @@ with the same code as described above.::
162162
]);
163163

164164
Finally clear all caches with ``bin/cake cache clear_all`` and try it out.
165+
166+
Manually updating counter caches
167+
================================
168+
169+
.. php:method:: updateCounterCache(?string $assocName = null, int $limit = 100, ?int $page = null): void
170+
171+
The ``updateCounterCache()`` method allows you to update the counter cache values
172+
for all records of one or all configured associations in batches. This can be useful,
173+
for example, to update the counter cache after importing data directly into the database.::
174+
175+
// Update the counter cache for all configured associations
176+
$table->updateCounterCache();
177+
178+
// Update the counter cache for a specific association, 200 records per batch
179+
$table->updateCounterCache('Articles', 200);
180+
181+
// Update only the first page of records
182+
$table->updateCounterCache('Articles', page: 1);
183+
184+
.. versionadded:: 5.2.0
185+
186+
.. note::
187+
188+
This methods won't update the counter cache values for fields which are
189+
configured to use a closure to get the count value.

0 commit comments

Comments
 (0)