*** THIS CHAPTER AND THE CODE IT REFERS TO IS UNDER CONSTRUCTION ***
Doctrine provides flexible event listener architecture that not only allows listening for different events but also for altering the execution of the listened methods.
There are several different listeners and hooks for various Doctrine components. Listeners are separate classes where as hooks are empty template methods which are defined in the base class.
Connection listeners are used for listening the methods of Doctrine_Connection and its modules (such as Doctrine_Transaction). All listener methods take one argument Doctrine_Event which holds information about the listened event.
+++ Creating a new listener
There are three different ways of defining a listener. First you can create a listener by making a class that inherits Doctrine_EventListener:
<code type='php'>
class MyListener extends Doctrine_EventListener
{
public function preExec(Doctrine_Event $event)
{
}
}
</code>
Note that by declaring a class that extends Doctrine_EventListener you don't have to define all the methods within the Doctrine_EventListener_Interface. This is due to a fact that Doctrine_EventListener already has empty skeletons for all these methods.
Sometimes it may not be possible to define a listener that extends Doctrine_EventListener (you might have a listener that inherits some other base class). In this case you can make it implement Doctrine_EventListener_Interface.
<code type='php'>
class MyListener implements Doctrine_EventListener_Interface
{
// notice: all listener methods must be defined here
// (otherwise PHP throws fatal error)
public function preExec(Doctrine_Event $event)
{ }
public function postExec(Doctrine_Event $event)
{ }
// ...
}
</code>
The third way of creating a listener is a very elegant one. You can make a class that implements Doctrine_Overloadable. This interface has only one method: __call(), which can be used for catching *all* the events.
<code type='php'>
class MyDebugger implements Doctrine_Overloadable
{
public function __call($methodName, $args)
{
print $methodName . ' called !';
}
}
</code>
+++ Attaching listeners
You can attach the listeners to a connection with setListener().
<code type='php'>
$conn->setListener(new MyDebugger());
</code>
If you need to use multiple listeners you can use addListener().
* preExecute() and postExecute() only get invoked when Doctrine_Connection::execute() is called without prepared statement parameters. Otherwise Doctrine_Connection::execute() invokes prePrepare, postPrepare, preStmtExecute and postStmtExecute.
All different event listeners in Doctrine allow chaining. This means that more than one listener can be attached for listening the same methods. The following example attaches two listeners for given connection:
<code type='php'>
// here Debugger and Logger both inherit Doctrine_EventListener
$conn->addListener(new Debugger());
$conn->addListener(new Logger());
</code>
++ Altering execution
Doctrine_Event provides many methods for altering the execution of the listened method as well as for altering the behaviour of the listener chain.
+++ skipOperation()
For some reason you may want to skip the execution of the listened method. It can be done as follows (note that preExec could be any listener method):
<code type='php'>
class MyListener extends Doctrine_EventListener
{
public function preExec(Doctrine_Event $event)
{
// some business logic, then:
$event->skipOperation();
}
}
</code>
+++ skipNextListener()
When using a chain of listeners you might want to skip the execution of the next listener. It can be achieved as follows: