简体   繁体   English

CakePHP 3:在组件中缓存数据库对象

[英]CakePHP 3: Caching database object in component

i'm trying to save an database object from the (plugin) component into the CakePHP Cache. 我正在尝试将数据库对象从(插件) 组件保存到CakePHP缓存中。

This works ( note the toArray() ) 这有效(注意toArray())

        $domains = Cache::read('domains', 'long');

        if ($domains === false) {

            $domainsTable = TableRegistry::get('DomainManager.Domains');
            $domains = $domainsTable->find('all', ['fields' => ['id', 'name']]);
            $domains = $domains->toArray();

            Cache::write('domains', $domains, 'long');
            return $domains;
        }

But this fails: 但这失败了:

        $domains = Cache::read('domains', 'long');

        if ($domains === false) {

        $domainsTable = TableRegistry::get('DomainManager.Domains');
        $domains = $domainsTable->find('all', ['fields' => ['id', 'name']]);

        Cache::write('domains', $domains, 'long');
        return $domains;
    }

The error given from CakePHP is Error: You cannot serialize or unserialize PDO instances CakePHP给出的错误是错误:您无法序列化或反序列化PDO实例

Sorry if i'm just doing something wrong, i just switched from Cake2 to Cake3 and could nothing find in the Documentation. 抱歉,如果我做错了什么,我刚从Cake2切换到Cake3,在文档中找不到任何内容。

Thanks for any glues! 感谢您的胶水!

The find function does not return results, it returns a query object that you can use to fetch results. find函数不返回结果,它返回可用于获取结果的查询对象。 Calling toArray on this will retrieve all the entities and give you something you can cache. 对此调用toArray将检索所有实体,并为您提供可以缓存的内容。 (It can be confusing, as toArray is also used to convert entities into arrays in other situations.) (这可能会造成混淆,因为在其他情况下toArray也用于将实体转换为数组。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM