Skip to content

Commit

Permalink
Merge pull request #7947 from cakephp/ADmad-patch-1
Browse files Browse the repository at this point in the history
Fix cross model event listener example
  • Loading branch information
markstory authored Nov 5, 2024
2 parents ffd9381 + 8d15afb commit a385e8a
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 a385e8a

Please sign in to comment.