【发布时间】:2015-06-13 14:33:25
【问题描述】:
Phalcon 1.3 MVC 模型文档说:
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’
我有一个模型类,它被序列化并实现了 onConstruct():
class Book extends \Phalcon\MVC\Model
{
protected $a;
protected $b;
function onConstruct() {
$this->a = 1;
}
function initialize() {
$this->b = 2;
}
}
Book 的多个实例存储在 memcached 中,然后在同一个请求中单独加载。
问题是从 memcached 读取(即反序列化)Book 的 onConstruct 永远不会被调用。
我还注意到,当连续反序列化多个 Book 时,它的 initialize() 仅在第一个实例中被调用(根据文档,这是正确的)。但是,当 Book #2 从 memcached 加载时,它的 $b 属性为空(预计为 2,正如文档所说:“适用于模型的所有实例”)。
问题:
- 为什么
onConstruct()根本不触发? - 为什么
Book的第二个实例没有将 $b 分配给 2?
谢谢!
【问题讨论】: