序列化Phalcon \ Mvc \ Model会丢失不属于架构的对象属性。
我有以下模型,在加载时设置状态数组:
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'];
}
}
}
我这样做:
$country = Country::findFirst($countryId);
$serialized = serialize($country);
$unserialized = unserialize($serialized);
$ serialized 字符串甚至不包含“states”子字符串。因此,在未序列化的对象中缺少“状态”。
我在会话中处理用户身份验证和持久性时发现了这一点(涉及序列化/反序列化)。我的用户对象丢失了在afterFetch()阶段加载的所有属性。
两个问题:
我在使用Phalcon 1.3.0。
谢谢, Temuri
答案 0 :(得分:4)
\ Phalcon \ Mvc \ Model实现Serializable接口。
要序列化您自己的属性(\ Phalcon \ Mvc \ Model不知道),您需要使用这样的技巧: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 as $key => $value) {
$this->$key = $value;
}
}
答案 1 :(得分:0)
答案是 - Phalcon序列化程序当前忽略所有非模型属性,以便使序列化对象变亮。
我已经提交了新的NFR:https://github.com/phalcon/cphalcon/issues/1285。