Skip to content

Commit

Permalink
Fix cross model event listener example
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad authored Nov 5, 2024
1 parent ffd9381 commit 8d15afb
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions en/development/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1830,11 +1830,15 @@ Expanding on the Orders example, say we have the following tables::

class CartsTable extends Table
{
public function implementedEvents(): array
public function initialize()
{
return [
'Model.Order.afterPlace' => 'removeFromCart'
];
// Models don't share the same event manager instance,
// so we need to use the global instance to listen to
// events from other models
\Cake\Event\EventManager::instance()->on(
'Model.Order.afterPlace',
callable: [$this, 'removeFromCart']
);
}

public function removeFromCart(EventInterface $event): void
Expand Down

0 comments on commit 8d15afb

Please sign in to comment.