Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
niden opened this issue Sep 29, 2022 Discussed in #16130 · 8 comments · Fixed by #16036
Closed

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

niden opened this issue Sep 29, 2022 Discussed in #16130 · 8 comments · Fixed by #16036
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report

Comments

@niden
Copy link
Member

niden commented Sep 29, 2022

Discussed in #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
Copy link
Member Author

niden commented Sep 29, 2022

@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 niden self-assigned this Sep 29, 2022
@niden niden added bug A bug report status: unverified Unverified 5.0 The issues we want to solve in the 5.0 release labels Sep 29, 2022
@niden niden changed the title Phalcon v5.0.2 + PHP 8.1 - Model caching error [BUG]: Phalcon v5.0.2 + PHP 8.1 - Model caching error Sep 29, 2022
@Alistar84
Copy link

Alistar84 commented Sep 30, 2022

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

@borisdelev
Copy link
Contributor

borisdelev commented Oct 10, 2022

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

@niden
Copy link
Member Author

niden commented Oct 10, 2022

I will check this out this week

@borisdelev
Copy link
Contributor

@niden thanks a lot!

@Jeckerson Jeckerson linked a pull request Oct 14, 2022 that will close this issue
5 tasks
@Jeckerson Jeckerson removed the status: unverified Unverified label Oct 14, 2022
@Jeckerson Jeckerson moved this to Backlog in Phalcon v5 Oct 14, 2022
@Jeckerson
Copy link
Member

Fixed in #16036

Repository owner moved this from Backlog to Implemented in Phalcon v5 Oct 14, 2022
@Jeckerson
Copy link
Member

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

@borisdelev
Copy link
Contributor

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

@niden niden moved this from Implemented to Released in Phalcon v5 Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants