Skip to content

[BUG]: onConstruct Model doesn't work as expected with cached instance of the model #15906

Open
@dz3n

Description

@dz3n

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions