Phalcon7 发布 Dao7-1.3.2,改进事件机制

模型的属性方法增加前缀“__”。

改进事件机制,能够针对部分操作对应事件进行拦截,并停止继续运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
$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());

返回:

1
2
3
4
5
6
7
8
array(2) {
["data"]=>
int(1)
["data2"]=>
int(2)
["data3"]=>
int(3)
}

坚持原创技术分享,您的支持将鼓励我继续创作!