We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Adding multiple EventManagers to DI

I'm wondering how to add Event Manager for only one or more models, not ALL

ex(from website).: this is globally, that will be invoked for ALL db actions

$dbListener = new MyDbListener();
// Listen all the database events
$eventsManager->attach('db', $dbListener);

$connection    = new DbAdapter([...]);

// Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);

How to properly add Event Manager to DI only for one model database events?

edited Mar '16

Well. Cant you just in initialize method in model use $this->setEventsManager() ?

There is example in docs:

https://docs.phalcon.io/pl/latest/reference/models.html#using-a-custom-events-manager

Assigning eventsManager to db or modelsManager will invoke eventsManager for all actions. But assigning it in intialize method for model will fix it.

Thank you, wondering how add eventManager for one or more CLI tasks, here is the global example https://forum.phalcon.io/discussion/870/event-after-task-in-console-application

Well. Cant you just in initialize method in model use $this->setEventsManager() ?

There is example in docs:

https://docs.phalcon.io/pl/latest/reference/models.html#using-a-custom-events-manager

Assigning eventsManager to db or modelsManager will invoke eventsManager for all actions. But assigning it in intialize method for model will fix it.