cphalcon
cphalcon copied to clipboard
[BUG]: onConstruct Model doesn't work as expected with cached instance of the model
Describe the bug Not able to initialize property in onConstruct if my model instance is from the cache
Provide minimal script to reproduce the issue
use Phalcon\Events\Manager;
use Phalcon\Mvc\Model;
class Foo extends Model
{
private Manager $eventManager;
public $name;
public function onConstruct()
{
$this->eventManager = Di::getDefault()->get('dispatcher')->getEventsManager();
}
public function afterUpdate()
{
$this->eventManager->fire('foo:updated', $this);
}
}
$foo = Foo::findFirst([
'conditions' => 'id = :id:',
'bind' => [
'id' => 1,
],
'cache' => array(
'key' => 'key.1',
)
]);
$foo->name = 'newName';
$foo->update();
//Fatal error: Uncaught Error: Typed property Foo::$eventManager must not be accessed before initialization
Expected behavior Properly triggered event listener afterUpdate
Details
- Phalcon version: 5.0.0beta2
- PHP Version: PHP 8.0.14
- Redis Version: 5.3.5
- Operating System: Ubuntu
- Installation type: Ubuntu + Docker
- Server: Nginx
related to https://github.com/phalcon/cphalcon/issues/15837 . unserialize object does not initialize itself in the same way as a normally constructed object (which triggers onConstruct)
i was using onConstruct to trigger
$this->getModelsManager()->keepSnapshots($this, $true);
but this is not happening on unserialize causing issues later on
would implementing the logic in __wakeup be an option?