cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Phalcon v5.0.0-alpha.2 - whats wrong with models cache?

Open borisdelev opened this issue 5 years ago • 12 comments

Describe the bug I try to migrate to Phalcon v.5 (php8) or just still try it with my project- need to be sure they will work fine with next releases. And yes, they works almost fine... except something with cache stuff. The error i get is: Fatal error: Uncaught TypeError: Phalcon\Mvc\Model\Resultset\Simple::__construct(): Argument #4 ($cache) must be of type ?Phalcon\Cache\Adapter\AdapterInterface, Phalcon\Cache given in v1\modules\client\controllers\ControllerBase.php on line 33

So, in this line i have:

        $articles = \Models\Articles::find([
            'conditions' => 'is_global=1',
            'cache' => [
                'key' => 'global-articles'
            ]
        ]);

... and see is something related with models cache service. Its init like that:

/*
 * Set the models cache service
 */
$di->set('modelsCache', function () use ($config) {
    $lifetime = 60 * 60 * 24 * 7; // 7 days
    if ($config->development) {
        $lifetime = 60; // 1 min
    }
    
    // Create folder if did not exists
    $folder = BASE_PATH . 'cache/queries/';

    $serializerFactory = new \Phalcon\Storage\SerializerFactory();
    $adapter = new \Phalcon\Cache\Adapter\Stream($serializerFactory, [
        'storageDir' => $folder,
        'lifetime' => $lifetime
    ]);
    
    $cache = new \Phalcon\Cache($adapter);

    return $cache;
});

Soo... maybe the mistake is Phalcon\Cache, but this works in Phalcon 4.x. So how must be changed?

Thanks :) And may the force be with you!

Details

  • Phalcon version: v5.0.0-alpha.2
  • PHP Version: 8.0.6
  • Operating System: Windows
  • Server: Apache

borisdelev avatar May 06 '21 13:05 borisdelev

Can you provide code part around this line? v1\modules\client\controllers\ControllerBase.php on line 33 I suppose you providing wrong object.

As is see in error, you trying to:

$simpleResultset = new Simple($columnMap, $model, $resultset, $this->modelsCache);

Can you try next change?

$simpleResultset = new Simple($columnMap, $model, $resultset, $this->modelsCache->getAdapter());

BeMySlaveDarlin avatar May 07 '21 20:05 BeMySlaveDarlin

Line 33 is that line with "key=" from ::find, but i will be more specific later, just to back home.

borisdelev avatar May 08 '21 10:05 borisdelev

image image

I think now its better. I did not have "$simpleResultset" but if u want me to try something... i am here! I will try to debug some more.

borisdelev avatar May 08 '21 12:05 borisdelev

image

borisdelev avatar May 08 '21 12:05 borisdelev

Can you try this one?

$di->set('modelsCache', function () use ($config) {
    $serializerFactory = new \Phalcon\Storage\SerializerFactory();

    return new \Phalcon\Cache\Adapter\Stream($serializerFactory, [
        'storageDir' => BASE_PATH . 'cache/queries/',
        'lifetime' => $config->development ? 60 : 60 * 60 * 24 * 7,
    ]);
});

BeMySlaveDarlin avatar May 08 '21 21:05 BeMySlaveDarlin

I confirm that this work in Phalcon 4x and Phalcon 5.0.0 alpha-2. If that is the way to set cache service- documentation must be fixed here: https://docs.phalcon.io/4.0/en/db-models-cache#resultsets (i can do that...). I am pretty sure there is more places with that service examples and i will find them.

I will test other type of cache to see if all works fine.

Well... thanks! A lot :) U awesome!

borisdelev avatar May 09 '21 08:05 borisdelev

If that is the way to set cache service- documentation must be fixed here...

Its only related to modelsCache service due to the way Query using it.

BeMySlaveDarlin avatar May 09 '21 10:05 BeMySlaveDarlin

@BeMySlaveDarlin so its not related with other cache stuff? Ok, cool. Thanks Anyway, is here must be fixed too: https://docs.phalcon.io/4.0/en/cache#cache-1 ?

borisdelev avatar May 09 '21 15:05 borisdelev

Nope, only modelsCache service. The rest of services works fine.

BeMySlaveDarlin avatar May 09 '21 16:05 BeMySlaveDarlin

Lov u guys xD ... Cant wait Phalcon 6... then i can contribute and help more.

Best wishes.

borisdelev avatar May 09 '21 16:05 borisdelev

I actually managed to reproduce this. It only happens in PHP 8 and the solution is to use CacheInterface vs AdapterInterface

I should have something ready very soon.

niden avatar May 10 '21 13:05 niden

Fixed in #15478

Jeckerson avatar May 10 '21 19:05 Jeckerson