php - Phalcon\Mvc\Model serialization - object properties are lost -


serializing phalcon\mvc\model loses object property that's not part of schema.

i have following model, upon load sets array of states:

class country extends phalcon\mvc\model {     protected $states;      public function initialize()     {         $this->setsource('countries');     }      public function afterfetch()     {         if ($this->id) {             $this->states = ['al', 'az', 'nv', 'ny'];         }     } } 

i this:

    $country = country::findfirst($countryid);     $serialized = serialize($country);     $unserialized = unserialize($serialized); 

$serialized string not contain "states" substring. hence, "states" missing in unserialized object.

i have discovered while working on user authentication , persistence in session (which involved serialization/unserialization). user object losing properties loaded in afterfetch() phase.

two questions:

  1. why did "states" property disappear upon serialization?
  2. is bad practice in phalcon world persist models (which thought convenient way of storing user object in session)?

i on phalcon 1.3.0.

thanks, temuri

\phalcon\mvc\model implements serializable interface.

to serialize own properties (which \phalcon\mvc\model unaware of), need use trick this: http://ua1.php.net/manual/en/class.serializable.php#107194

public function serialize() {     $data = array(         'states' => $this->states,         'parent' => parent::serialize(),     );      return serialize($data); }  public function unserialize($str) {     $data = unserialize($str);     parent::unserialize($data['parent']);     unset($data['parent']);     foreach ($data $key => $value) {         $this->$key = $value;     } } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -