Phalcon v.1.3. Model's onConstruct does not fire after unserialize

119 views Asked by At

Phalcon 1.3 MVC Model docs say:

The initialize() method is only called once during the request, it’s intended to perform initializations that apply for all instances of the model created within the application. If you want to perform initialization tasks for every instance created you can ‘onConstruct’

I have a Model class that gets serialized and implements onConstruct():

class Book extends \Phalcon\MVC\Model
{
     protected $a;
     protected $b;

     function onConstruct() {
          $this->a = 1;
     }

     function initialize() {
          $this->b = 2;
     }
}

Multiple instances of Book are stored in memcached and then loaded back individually within the same request.

The problem is that upon read from memcached (i.e. unserialization) Book's onConstruct never gets called.

I have also noticed that when unserializing more than one Book in a row, its initialize() gets called only for the first instance (which is correct, according to the docs). However, when Book #2 is loaded from memcached, it's $b property is null (expected to be 2, as docs say: "apply for all instances of the model").

Questions:

  1. Why doesn't onConstruct() fire at all?
  2. Why doesn't the second instance of Book get $b assigned to 2?

Thanks!

1

There are 1 answers

0
yergo On

I may guess that serialized/unserialized model is considered to be already initialized, so if you need some additional tricks, you have to do them after unserialization, eg. during unserialize method.

Anyway according to upper logic, I would consider of reporting a bug. If initialize method is called once, it should be called every object unserialization OR none at all, to make thing consistent.

I may test this out with working example.