phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

[BUG]: find and findFirst fails with exception from Phalcon\Mvc\Model->toArray if there is APCU model cache enabled #15711

Open elcreator opened 3 years ago

elcreator commented 3 years ago

Describe the bug find and findFirst fails with exception from Phalcon\Mvc\Model->toArray if there is APCU model cache enabled.

To Reproduce [06-Oct-2021 23:20:27 Europe/Kiev] PHP Fatal error: Uncaught Error: Typed property Auth\Models\User::$email must not be accessed before initialization in /home/project/includes/BaseModel.php:33 Stack trace:

0 /home/project/includes/BaseModel.php(33): Phalcon\Mvc\Model->toArray()

1 [internal function]: BaseModel->toArray()

2 [internal function]: Phalcon\Mvc\Model->serialize()

3 [internal function]: serialize()

4 [internal function]: Phalcon\Mvc\Model\Resultset\Simple->serialize()

5 [internal function]: serialize()

6 [internal function]: Phalcon\Storage\Serializer\Php->serialize()

7 [internal function]: Phalcon\Storage\Adapter\AbstractAdapter->getSerializedData()

8 [internal function]: Phalcon\Storage\Adapter\Apcu->set()

9 [internal function]: Phalcon\Cache->set()

10 [internal function]: Phalcon\Mvc\Model\Query->execute()

11 /home/project/includes/BaseModel.php(112): Phalcon\Mvc\Model::findFirst()

12 /home/project/modules/auth/controllers/LoginController.php(22): BaseModel::findFirst()

13 [internal function]: Auth\Controllers\LoginController->login in /home/project/includes/BaseModel.php on line 33

Steps to reproduce the behavior: https://github.com/elcreator/phalcon-demo/blob/main/includes/BaseModel.php#L117 - uncomment this to see the failure. It works if these lines remain commented (i.e. when cache parameter is absent). Refer to https://docs.phalcon.io/4.0/en/db-models-cache example as cache was taken from this doc

Expected behavior Error should not be thrown

Details

Additional context https://github.com/elcreator/phalcon-demo follow readme to get environment with DB to reproduce this issue when trying to login

Get familiar with https://github.com/elcreator/phalcon-demo/blob/main/modules/auth/controllers/LoginController.php https://github.com/elcreator/phalcon-demo/blob/main/modules/auth/models/EmailUser.php https://github.com/elcreator/phalcon-demo/blob/main/modules/auth/models/User.php

BeMySlaveDarlin commented 3 years ago

This is related to how typed properties works in php7.4+ and the way we initialize models. For now, to fix this issue, we need to change the logic of models initializasion in Phalcon\Mvc\Model::unserialize(). Highly likely it will cause bbc.