您无法序列化或反序列化PDO实例CakePHP

时间:2017-04-14 14:49:01

标签: php cakephp caching pdo

我的cakephp出了问题,我不知道如何解决,我试图更新供应商,但错误仍在继续。

error.log中


    Error: [PDOException] You cannot serialize or unserialize PDO instances
    Request URL: /
    Stack Trace:
    #0 [internal function]: PDO->__sleep()
    #1 vendor/cakephp/cakephp/src/Cache/Engine/FileEngine.php(148): serialize(Object(Cake\ORM\Query))
    #2 vendor/cakephp/cakephp/src/Cache/Cache.php(239): Cake\Cache\Engine\FileEngine->write('cake_views_post...', Object(Cake\ORM\Query))
    #3 src/Controller/PostsController.php(494): Cake\Cache\Cache::write('ViewsPosts', Object(Cake\ORM\Query))
    #4 src/Controller/PostsController.php(57): App\Controller\PostsController->getPostsSidebar()
    #5 vendor/cakephp/cakephp/src/Controller/Controller.php(262): App\Controller\PostsController->initialize()
    #6 [internal function]: Cake\Controller\Controller->__construct(Object(Cake\Network\Request), Object(Cake\Network\Response), 'Posts')
    #7 vendor/cakephp/cakephp/src/Http/ControllerFactory.php(79): ReflectionClass->newInstance(Object(Cake\Network\Request), Object(Cake\Network\Response), 'Posts')
    #8 vendor/cakephp/cakephp/src/Http/ActionDispatcher.php(93): Cake\Http\ControllerFactory->create(Object(Cake\Network\Request), Object(Cake\Network\Response))
    #9 vendor/cakephp/cakephp/src/Http/BaseApplication.php(83): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
    #10 vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
    #11 vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php(62): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
    #12 vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
    #13 vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
    #14 vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
    #15 vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php(81): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
    #16 vendor/cakephp/cakephp/src/Http/Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response), Object(Cake\Http\Runner))
    #17 vendor/cakephp/cakephp/src/Http/Runner.php(51): Cake\Http\Runner->__invoke(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
    #18 vendor/cakephp/cakephp/src/Http/Server.php(92): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
    #19 webroot/index.php(37): Cake\Http\Server->run()
    #20 {main}

作曲家秀

    $ composer show
    aura/installer-default                1.0.0
    aura/intl                             1.1.1
    cakephp/bake                          1.2.8
    cakephp/cakephp                       3.3.7
    cakephp/chronos                       1.0.0
    cakephp/debug_kit                     3.3.7
    cakephp/migrations                    1.6.3
    cakephp/plugin-installer              0.0.15
    dnoegel/php-xdg-base-dir              0.1
    jakub-onderka/php-console-color       0.1    
    jakub-onderka/php-console-highlighter v0.3.2
    jdorn/sql-formatter                   v1.2.17
    mobiledetect/mobiledetectlib          2.8.22
    User-Agent strin...
    nikic/php-parser                      v2.1.1
    psr/http-message                      1.0.1
    psr/log                               1.0.0
    psy/psysh                             v0.7.2
    robmorgan/phinx                       v0.5.3
    symfony/config                        v3.1.6
    symfony/console                       v3.1.6
    symfony/debug                         v3.1.6
    symfony/filesystem                    v3.1.6
    symfony/polyfill-mbstring             v1.2.0
    symfony/var-dumper                    v3.1.6
    symfony/yaml                          v3.1.6
    zendframework/zend-diactoros          1.3.7

修改

SRC /控制器/ PostsController.php

if (($ViewsPosts = Cache::read('ViewsPosts')) === false) {

    $ViewsPosts = TableRegistry::get('ViewsPosts')->find('all')
        ->select([
            'count' => 'COUNT(*)',
            'Posts.created',
            'Posts.id',
        ])
        ->contain([
            'Posts'
        ])
        ->where([
            'Posts.status'        => 'published',
            'Posts.created >=' => new \DateTime('-7 days')
        ])
        ->group(['posts_id'])
        ->order(['count' => 'desc'])
        ->limit(5);

    Cache::write('ViewsPosts', $ViewsPosts); /// line 494
}

FIXED LINE

- Cache::write('ViewsPosts', $ViewsPosts);
+ Cache::write('ViewsPosts', $ViewsPosts->toArray());

1 个答案:

答案 0 :(得分:0)

您的错误是由此文件引起的:

src/Controller/PostsController.php

494

您正在调用Cake\Cache\Cache::write('ViewsPosts', Object(Cake\ORM\Query)),但它不喜欢这样,因为您无法序列化Cake \ ORM \ Query(这可能被称为$results或其他东西,所以不要这样做,而是想做。

$results->cache('ViewPosts');将为您缓存结果。您可以从CakePHP网站获得更多相关信息

Caching Loaded Results - 内容也包含在下方。

缓存已加载的结果

获取不经常更改的实体时,您可能希望缓存结果。 Query类使这很简单:

$query->cache('recent_articles');

将在查询的结果集上启用缓存。如果只为cache()提供了一个参数,那么将使用'default'缓存配置。您可以使用第二个参数控制使用哪种缓存配置:

// String config name.
$query->cache('recent_articles', 'dbResults');

// Instance of CacheEngine
$query->cache('recent_articles', $memcache);

除了支持静态密钥之外,cache()方法还接受一个生成密钥的函数。您提供的函数将接收查询作为参数。然后,您可以读取查询的各个方面以动态生成缓存键:

// Generate a key based on a simple checksum
// of the query's where clause
$query->cache(function ($q) {
    return 'articles-' . md5(serialize($q->clause('where')));
});

缓存方法可以将缓存结果添加到自定义查找器或通过事件侦听器。

当获取缓存查询的结果时,会发生以下情况:

  1. 触发Model.beforeFind事件。
  2. 如果查询已设置结果,则会返回这些结果。
  3. 将解析缓存密钥并读取缓存数据。如果缓存数据不为空,则返回这些结果。
  4. 如果缓存未命中,将执行查询并创建新的ResultSet。此ResultSet将写入缓存并返回。
  5.   

    您无法缓存流式查询结果。