How to save custom field into memcache?

Code:

$user = Users::findFirst(1);
$user->myvar = '123456';
$this->Memcached->save('user',$user,86400);
$myuser = $this->Memcached->get('user');
var_dump($myuser);die;

The object user has not myvar field.



82.7k

Mvc\Model have its own serializer that only serializes the attributes provided by the model's meta-data, you can override that serializer to include additional variables:

<?php

class Users extends Phalcon\Mvc\Model
{

    /**
     * Serializes the object ignoring connections or static properties
     *
     * @return string
     */
    public function serialize()
    {
        $data = array();
        foreach (get_object_vars($this) as $var => $value) {
            if (!is_object($value)) {
                $data[$var] = $value;
            }
        }       
        return serialize($data);
    }

}