Trying to make an event listener that will work after the data is saved in the table cant find a way to make it work . Pasting the code below can anyone help me ??
Model Code :
namespace App\Model\Table;
namespace App\Event;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class ExExchangeMatchupTable extends Table {
public function initialize(array $config) {
$this->setTable('ex_exchange_matchup');
// $this->hasMany('EmployeeRole')
// ->setForeignKey('employee_id')
// ->setConditions(['EmployeeRole.role !=' => 'Employee']);
// use Cake\Event\EventManager;
}
public function place($ex_exchange_matchup)
{
if ($this->save($ex_exchange_matchup)) {
// $this->ExExchangeMatchupTable->remove($ex_exchange_matchup);
$event = new Event('Model.ExExchangeMatchup.afterPlace', $this, [
'ex_exchange_matchup' => $ex_exchange_matchup
]);
$this->getEventManager()->dispatch($event);
return true;
}
return false;
}
}
//////////////////////////////////////////////
Listener code
use Cake\Event\EventListenerInterface;
use Cake\Mailer\Email;
use Cake\Mailer\TransportFactory;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;
class ExExchangeMatchupStatus implements EventListenerInterface
{
public function implementedEvents()
{
return [
‘Model.ExExchangeMatchup.afterPlace’ => ‘updateBuyStatistic’,
];
}
public function updateBuyStatistic($event)
{
$text ='This is a listener log for request';
if ($text) {
$txt = date('Y-m-d h:i:s');
$text = $text . '-' . $txt . PHP_EOL . '=================================================================';
$myfile1 = file_put_contents('service-logs/Exchange/listener-logs - ' . date('m-Y') . '.txt', $text . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
}
// Attach the UserStatistic object to the Order’s event manager
$statistics = new \ExExchangeMatchupStatus();
$this->ExExchangeMatchup->getEventManager()->on($statistics);
I have made a folder named Event in the src and have placed the listener code there.
I cant find a way to bind the model and listener .