首页 > 解决方案 > TYPO3 9.5 - 我的扩展中的 QuerieResult 缓存 - 函数 map() on null

问题描述

我正在尝试让 Produclist 缓存在我的插件中工作。

我的 ext_localconf.php

if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['product_cache'])) {
    $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['product_cache'] = [];
}
if( !isset($GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations']['product_cache']['frontend'] ) ) {
    $GLOBALS['TYPO3_CONF_VARS'] ['SYS']['caching']['cacheConfigurations']['product_cache']['frontend'] = 'TYPO3\\CMS\\Core\\Cache\\Frontend\\VariableFrontend';
}

还有我的控制器

$cache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('product_cache');

if(($products = $cache->get($cacheIdentifier)) === FALSE){

    $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
    $productController = $objectManager->get(ProductController::class);
    $productController->setSettings($this->settings);
    $products = $productController->getEditedProducts($catId);
    $cache->set($cacheIdentifier, $products, ['productajax'], 84600);

}

像字符串、整数或数组这样的普通内容可以正常工作,但是当我使用 DatabaseResultquerie 尝试此操作时,系统会因此错误而崩溃:Call to a member function map() on null

在此处输入图像描述

(仅在获取时,设置工作正常)

标签: phpcachingtypo3extbasetypo3-9.x

解决方案


您不能缓存此类,因为这意味着对其进行序列化,并且该类包含阻止某些属性包含在序列化字符串中的显式方法。事实上,唯一包含的属性是query导致结果的输入查询)。

您可能能够缓存 QueryResult,然后手动调用注入方法来添加 DataMapper 等实例 - 但即使您这样做了,序列化的 QueryResult 也不会包含结果,并且每次您尝试从中加载实体时都会再次触发它。

正确的方法是提取到您知道将允许序列化结果的非 QueryResult(数组,自己选择的迭代器)。

请参阅:https ://github.com/TYPO3/TYPO3.CMS/blob/v8.7.26/typo3/sysext/extbase/Classes/Persistence/Generic/QueryResult.php#L250


推荐阅读