CakePHP 3 events -


i want create entry in notifications table if particular find method has return value on contacts table.

so in contactstable create event.

use cake\event\event;  public function checkduplicates() {     //... code here     $event = new event('model.contacts.afterduplicatescheck', $this, [             'duplicates' => $duplicates         ]);     $this->eventmanager()->dispatch($event); } 

i have created contactslistener.php @ /src/event

namespace app\event;  use cake\event\event; use cake\event\eventlistenerinterface; use cake\log\log;  class contactslistener implements eventlistenerinterface {      public function implementedevents()     {         return [             'model.contacts.afterduplicatescheck' => 'createnotificationaftercheckduplicates',         ];     }      public function createnotificationaftercheckduplicates(event $event, array $duplicates)     {         log::debug('here am');     } } 

in notificationstable.php have following code.

public function initialize(array $config) {     $this->table('notifications');     $this->displayfield('id');     $this->primarykey('id');      $listener = new contactslistener();     $this->eventmanager()->on($listener); } 

i guess part problem, never log entries. cookbook not enough clear , code found not same cookbook describes, cake 3.

how , should attach listener?

you working 2 separate local event manager instances, never hear each other. either have explicitly subscribe manager on contactstable instance, or use global event manager gets notified events:

[...]

each model has separate event manager, while view , controller share one. allows model events self contained, , allow components or controllers act upon events created in view if necessary.

global event manager

in addition instance level event managers, cakephp provides global event manager allows listen event fired in application.

[...]

cookbook > events system > accessing event managers

so, either like

\cake\orm\tableregistry::get('contacts')->eventmanager()->on($listener); 

which work until registry being cleared, or subscribe globally

\cake\event\eventmanager::instance()->on($listener); 

on side note

in order work @ all, notificationstable class must instantiated somewhere. so, i'd suggest wrap in utitlity class, or maybe component, listen event instead, , use notificationstable save notifications.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -