#95899Try to cache an extbase queryResult leads to "Serialization of 'Closure' is not allowed"
Description
I would like to add some caching into my backend views in lux to speed up diagrams.
But in TYPO3 11.5.2 together (with PHP 7.4 or 8.0) I always ran into the exception:
“Serialization of ‘Closure’ is not allowed”
An example extbase model could be very simple (without any relations or Lazy Loading) with just one string property and extend AbstractEntity. Also the repository function is without magic here:
$query = $this->createQuery(); $query->setLimit(3); $query->setOrderings(['uid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING]); return $query->execute();
If I try to cache the result like
$cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('bar');
$cacheInstance->set('foo', $queryResult, ['bar'], 100000);
The error is happening:
Any ideas?
Reviews 4
Related issues 1
Files 4
History 510
-
Benni Mack changed Related to #91364 <span data-processor="relativetime" data-value="2022-12-21T08:16:53+00:00" > 12/21/22, 08:16 </span>
-
Marvin Buchmann commented and changed attached File.php attached SerializableEntityInterface.php attached SerializableEntityTrait.php <span data-processor="relativetime" data-value="2022-12-21T08:52:59+00:00" > 12/21/22, 08:52 </span>
Alex Kellner wrote in #note-1:
> Update: Using an array instead of QueryResult helps as workaround:
> […]Thanks! I had the same problem when trying to cache my QueryResult. But the workaround only seems to work if the model doesn’t contain any (lazy) ObjectStorage properties (tested in TYPO3 11.5).
Since I needed those, I created a workaround that works for all my models. I got the idea from this fix https://review.typo3.org/c/Packages/TYPO3.CMS/+/68341.It’s basically just an interface and a trait unsetting all ObjectStorage properties by default when serialized and calling the initializeObject method of the object when unserialized. If not implemented, it provides one that just initializes the ObjectStorage properties empty.
The model has to implement the interface and use the trait. Then it’s serializable and therefore cacheable. :) At least in my use cases.
I attached an example model, the interface and the trait.I know traits are not really desirable, but the methods could be implemented in the AbstractEntity class if it works for all other use cases aswell. Then every extbase model would be cacheable by default, which I would really appreciate.
-
Georg Ringer commented <span data-processor="relativetime" data-value="2026-08-27T19:51:33+00:00" > 08/27/26, 19:51 </span>
I traced this on current main (v15-dev). The chain is verified with a functional
test that walks the object graph honouring__sleep()and reports every Closure:$queryResult -> QueryResult::$query (__sleep => ['query']) -> Query::$querySettings (__sleep includes 'querySettings') -> Typo3QuerySettings::$configurationManager <-- no __sleep, this is where it leaks -> ConfigurationManager::$feConfigManager -> FrontendConfigurationManager::$flexFormTools -> FlexFormTools::$eventDispatcher -> EventDispatcher::$listenerProvider -> ListenerProvider::$container -> DependencyInjectionContainer_...::$getService [Closure]
QueryResultandQueryboth do their homework: they have__sleep()/@__wakeup()@
and keep their DI dependencies out. @Typo3QuerySettings@ has none, so the whole
compiled Symfony container hangs off the object graph through
@$configurationManager@. That is the entire bug — the domain models are innocent,
which is exactly whytoArray()works.Four things not yet mentioned in this issue
1. Both properties are dead code.
Typo3QuerySettingsnever reads$configurationManageror$contextafter the
constructor. Complete list of occurrences in the class::93 $this->context = clone $context; :94 $this->configurationManager = $configurationManager; :95 $this->languageAspect = $this->context->getAspect('language');So the container is dragged along for nothing.
2.
Query::__wakeup()is broken on its own, independent of the Closure problem.// Query.php:649 $this->persistenceManager = GeneralUtility::makeInstance(PersistenceManagerInterface::class); $this->dataMapFactory = GeneralUtility::makeInstance(DataMapFactory::class); // throws $this->qomFactory = GeneralUtility::makeInstance(QueryObjectModelFactory::class);PersistenceManagerInterfaceis registeredpublic: truein
@extbase/Configuration/Services.yaml:17@,DataMapFactoryis not. For private
servicesmakeInstance()falls back tonew $className(), and
@DataMapFactory::__construct()@ takes six arguments. Reproduced:ArgumentCountError: Too few arguments to function DataMapFactory::__construct(), 0 passed in GeneralUtility.php on line 2489 and exactly 6 expected
So even with the Closure issue solved, the wake-up path is dead. The correct call
already exists two classes away, inLazyObjectStorage::__unserialize():314—
@GeneralUtility::getContainer()->get(DataMapper::class)@.3.
Query::__wakeup()does not restore$container.$container(Query.php:78) was added later whenexecute()was switched over to
the container (Query.php:217) and was forgotten in__wakeup(). After waking up,
@execute()@ would hit “Typed property must not be accessed before initialization”.4.
QueryResult::__wakeup()silently loses the language configuration.setQuery()(QueryResult.php:65) normally does two things: remember the query
*and* call$this->dataMapper->setQuery($query).__wakeup()only does the first.
Verified: after waking up,DataMapper::$queryisnull. Consequences:DataMapper.php:184—$this->query?->getQuerySettings()->getLanguageAspect()
falls back tonew LanguageAspect(), so a cached result would be mapped with
default language behaviour instead of the query’s.DataMapper.php:471—setParentQuery()is skipped, lazy relations lose their
query context.
Not a crash, a silent correctness bug. Nobody notices today because you never get
that far.Marvin’s ObjectStorage concern is obsolete
The very change referenced in note-3 (68341 landed:
LazyObjectStorage::__serialize():298and
@LazyLoadingProxy::__serialize():235@ drop theDataMapperand fetch it from the
container on wake-up. Verified on main withblog_example(Blog::$postsis
@#[Lazy]@):serialize(toArray()): OK, 3322 bytes unserialize: OK lazy posts after unserialize: LazyObjectStorage, count=10
toArray()serialises cleanly today, lazy relations included, and lazy loading
still works afterwards. The attached interface + trait are no longer needed.The bigger point: a cached QueryResult caches no data
QueryResult::__sleep()returns['query'].$queryResult(the mapped objects) and
@$numberOfResults@ are dropped. What ends up in the cache is the query, not the
result — on first access after waking up,initialize()runs the database query
again.The original goal here was “add some caching into my backend views in lux to speed
up diagrams”. Even with working serialisation that would have gained nothing.
@toArray()@ is not a workaround, it is the correct answer for that use case.Suggested way forward
- Give
Typo3QuerySettingsa__sleep()returning only its own scalar state plus
languageAspect, and a__wakeup()restoringContext(SingletonInterface, no
ctor args) andConfigurationManagerInterface(Services.yaml:26,public: true)
— both reachable viamakeInstance(). Non-breaking and backportable. - Fix the three broken wake-up spots above, otherwise one exception is just traded
for the next. - Document that
toArray()is the right call when you want to cache the data.
The alternative would be to declare
QueryResultoff-limits for caching altogether
and have__sleep()throw a descriptive exception (“use toArray() to cache the
result set”). That solves the reported use case better, but blocks legitimate cases
such as caching a query for re-execution in a later request. That decision should be
made before anyone writes the patch — happy to provide it either way.Code is identical on main, 14.3 and 13.4 (line offsets only), so a fix is
backportable to both.#91364 has the same root cause and could be
closed as a duplicate of this one. -
Gerrit Code Review pushed patch set 1 for main to change 95458 status
NewUnder Review <span data-processor="relativetime" data-value="2026-08-27T20:06:46+00:00" > 08/27/26, 20:06 </span> -
Gerrit Code Review pushed patch set 1 for 14.3 to change 95525 <span data-processor="relativetime" data-value="2026-09-01T19:17:53+00:00" > 09/01/26, 19:17 </span>
-
Gerrit Code Review pushed patch set 1 for 13.4 to change 95526 <span data-processor="relativetime" data-value="2026-09-01T19:18:08+00:00" > 09/01/26, 19:18 </span>
-
Gerrit Code Review pushed patch set 2 for 13.4 to change 95526 <span data-processor="relativetime" data-value="2026-09-01T19:36:38+00:00" > 09/01/26, 19:36 </span>
-
Gerrit Code Review pushed patch set 2 for 14.3 to change 95525 <span data-processor="relativetime" data-value="2026-09-01T19:48:11+00:00" > 09/01/26, 19:48 </span>
-
Georg Ringer commented and changed status
Under ReviewResolved done0 %100 % <span data-processor="relativetime" data-value="2026-09-01T20:05:09+00:00" > 09/01/26, 20:05 </span>Applied in changeset commit:0dfa5ff2b0a5fa4311ee8a9105fdd86578311641.
Update: Using an array instead of QueryResult helps as workarround: