如何在 cakephp 3 中分离全局事件
how to deattach global event in cakephp 3
为我的查找查询创建了一个全局 beforeFind() 侦听器,但需要为某些请求分离它。.
控制器中的 $this->eventManager()->off() 不工作。即不取消附加事件。
在我的 bootstrap.php 文件中:
$modelListerner = new DeletedListener(); //my custom listerner
EventManager::instance()->on(
$modelListerner
);
您不能在本地分离全局侦听器,您必须在全局分离它,即通过
EventManager::getInstance()->off(/* ... */);
但是你可能想考虑将选项传递给查找器是否是一个更好的解决方案,这样你的控制器就不必知道监听器等,而只需根据需要执行查找调用,比如
$Table->find('all', ['doThisAndThat' => false]);
然后您的听众可以相应地采取行动。
引自文档
[...] Any options that are not in this list will be passed to beforeFind listeners where they can be used to modify the query object.
Cookbook > Database Access & ORM > Retrieveing Data & Result Sets > Using Finders to Load Data
为我的查找查询创建了一个全局 beforeFind() 侦听器,但需要为某些请求分离它。. 控制器中的 $this->eventManager()->off() 不工作。即不取消附加事件。 在我的 bootstrap.php 文件中:
$modelListerner = new DeletedListener(); //my custom listerner
EventManager::instance()->on(
$modelListerner
);
您不能在本地分离全局侦听器,您必须在全局分离它,即通过
EventManager::getInstance()->off(/* ... */);
但是你可能想考虑将选项传递给查找器是否是一个更好的解决方案,这样你的控制器就不必知道监听器等,而只需根据需要执行查找调用,比如
$Table->find('all', ['doThisAndThat' => false]);
然后您的听众可以相应地采取行动。
引自文档
[...] Any options that are not in this list will be passed to beforeFind listeners where they can be used to modify the query object.
Cookbook > Database Access & ORM > Retrieveing Data & Result Sets > Using Finders to Load Data