[BUG]: Phalcon v5.0.2 + PHP 8.1 - Model caching error
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?
@Alistar84 could you please post a sample model and code with some potential data? I want to create a test to diagnose this.
Thanks
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
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

I will check this out this week
@niden thanks a lot!
Fixed in https://github.com/phalcon/cphalcon/pull/16036
@borisdelev please confirm if it fixed in v5.0.4.
@Jeckerson yes, it work. Just update the hosting with 5.0.4 :) Thanks for everything :) Greetings!