模型的属性方法增加前缀“__”。 改进事件机制,能够针对部分操作对应事件进行拦截,并停止继续运行: $eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
if (!$prevdata) {
return ['data' => 1];
}
return $prevdata;
});
$eventsManager->attach('model:beforeQuery', function($event, $model, $data, $prevdata) {
if (!$prevdata) {
return [];
}
$prevdata['data2'] = 2;
return $prevdata;
});
$di = new Phalcon\Di\FactoryDefault;
$di->set('modelsManager', function() use ($eventsManager) {
$modelsManager = new Phalcon\Mvc\Model\Manager();
$modelsManager->setEventsManager($eventsManager);
return $modelsManager;
}, true);
class Robots extends Phalcon\Mvc\Model {
public function beforeQuery($event, $data, $prevdata) {
if (!$prevdata) {
return [];
}
$prevdata['data3'] = 3;
return $prevdata;
}
}
var_dump(Robots::find()); 返回: array(2) { ["data"]=> int(1) ["data2"]=> int(2) ["data3"]=> int(3) } |