cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Phalcon v5.0.2 + PHP 8.1 - Model caching error

Open niden opened this issue 4 years ago • 8 comments

Discussed in https://github.com/phalcon/cphalcon/discussions/16130

Originally posted by Alistar84 September 29, 2022 Hello everybody, I have a problem with model caching.

In v4 all my models extend a class that implements a force cache system similar to this one https://docs.phalcon.io/5.0/en/db-models-cache#forcing-cache

Now, in v5, if I run a find or findFirst that is already cached, I've got this error: Fatal error: Uncaught TypeError: unserialize(): Argument #1 ($data) must be of type string, array given.

How can I solve?

niden avatar Sep 29 '22 18:09 niden

@Alistar84 could you please post a sample model and code with some potential data? I want to create a test to diagnose this.

Thanks

niden avatar Sep 29 '22 18:09 niden

After some investigation, I realized that the problem occurs only if in the column list there are values ​​like CONCAT, DISTINCT, GROUP_CONCAT, COUNT, ...

This is the code:

BaseModel

class BaseModel extends \Phalcon\Mvc\Model {
	
	[...]

	public static function find($parameters = null): ResultsetInterface {
		$parameters = self::excludeSoftDelete($parameters);
		return parent::find($parameters);
	}
	
	public static function findFirst($parameters = null): ?ModelInterface {
		$parameters = self::excludeSoftDelete($parameters);
		return parent::findFirst($parameters);
	}
	
	protected static function forceCache(string $key, $parameters = null): array{
	    if($parameters !== null){
	        if(is_array($parameters) !== true) $parameters = [$parameters];
	        if(isset($parameters['cache']) !== true) $parameters['cache'] = ['key' => $key, 'lifetime' => 86400];
	    }else{
	        $parameters = ['cache' => ['key' => $key, 'lifetime' => 86400]];
	    }
	    
	    return $parameters;
	}
}

Robots model

class Robots extends BaseModel
{
    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(column="id", type="integer", length=2, nullable=false)
     */
    public $id;

    /**
     *
     * @var integer
     * @Column(column="id_status", type="integer", length=1, nullable=false)
     */
    public $id_status;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("robots");
    }

    public static function find($parameters = null): \Phalcon\Mvc\Model\ResultsetInterface
    {
        $key = 'Robots_find_'.md5(json_encode($parameters));
        $parameters = parent::forceCache($key, $parameters); 
        return parent::find($parameters);
    }

    public static function findFirst($parameters = null): ?\Phalcon\Mvc\ModelInterface
    {
        $key = 'Robots_findFirst_'.md5(json_encode($parameters));
        $parameters = parent::forceCache($key, $parameters);
        return parent::findFirst($parameters);
    }
}

Controller

$data = Robots::find(['columns' => ['COUNT(id)'], 'conditions' => 'id_status = 2']);

Phalcon version: 5.0.2 PHP Version: 8.1.1 Server: Apache

Alistar84 avatar Sep 30 '22 07:09 Alistar84

I have same issue in 5.0.3. There is a quick code to check (in some controller, and you can change with your infrastructure models):

        $builder = $this->modelsManager->createBuilder();
        $builder->columns('v.*, p.*');
        $builder->addFrom('Models\ProductVariants', 'v');
        $builder->join('Models\Products', 'p.id=v.product_id', 'p');
        $builder->getQuery()->cache(['key' => 'product-test-relation', 'lifetime' => 1])->execute();

So... If you change $builder->columns('v.*, p.*'); to $builder->columns('v.*'); will work fine. But if you return p.* error ocured.

Update: upload a screenshot image

borisdelev avatar Oct 10 '22 14:10 borisdelev

I will check this out this week

niden avatar Oct 10 '22 14:10 niden

@niden thanks a lot!

borisdelev avatar Oct 10 '22 14:10 borisdelev

Fixed in https://github.com/phalcon/cphalcon/pull/16036

Jeckerson avatar Oct 14 '22 14:10 Jeckerson

@borisdelev please confirm if it fixed in v5.0.4.

Jeckerson avatar Oct 18 '22 09:10 Jeckerson

@Jeckerson yes, it work. Just update the hosting with 5.0.4 :) Thanks for everything :) Greetings!

borisdelev avatar Oct 18 '22 09:10 borisdelev