++ Introduction

*** 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.

An example of using Doctrine_Record hooks:
<code type='php'>
class Blog extends Doctrine_Record 
{
    public function setTableDefinition()
    {
        $this->hasColumn('title', 'string', 200);
        $this->hasColumn('content', 'string');
        $this->hasColumn('created', 'date');
    }
    public function preInsert()
    {
        $this->created = date('Y-m-d', time());
    }
}

// initialize connection etc, then:

$blog = new Blog();
$blog->title = 'New title';
$blog->content = 'Some content';
$blog->save();

$blog->created; // 2007-06-20 (format: YYYY-MM-DD)
</code>
++ Connection listeners    

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().

<code type='php'>
$conn->addListener(new MyDebugger());
$conn->addListener(new MyLogger());
</code>

+++ preConnect, postConnect

+++ Transaction listeners
||~ Methods     ||~ Listens ||~ Params ||
|| preTransactionBegin() || Doctrine_Transaction::beginTransaction() || ||
|| postTransactionBegin() || Doctrine_Transaction::beginTransaction() || ||
|| preTransactionRollback() || Doctrine_Transaction::rollback() || ||
|| postTransactionRollback() || Doctrine_Transaction::rollback() || ||
|| preTransactionCommit() || Doctrine_Transaction::commit() || ||
|| postTransactionCommit() || Doctrine_Transaction::commit() || ||
|| preCreateSavepoint() || Doctrine_Transaction::createSavepoint() || savepoint ||
|| postCreateSavepoint() || Doctrine_Transaction::createSavepoint() || savepoint ||
|| preRollbackSavepoint() || Doctrine_Transaction::rollbackSavepoint() || savepoint ||
|| postRollbackSavepoint() || Doctrine_Transaction::rollbackSavepoint() || savepoint ||
|| preReleaseSavepoint() || Doctrine_Transaction::releaseSavepoint() || savepoint ||
|| postReleaseSavepoint() || Doctrine_Transaction::releaseSavepoint() || savepoint ||

+++ Query execution listeners

||~ Methods     ||~ Listens ||~ Params ||
|| prePrepare() || Doctrine_Connection::prepare() || query ||
|| postPrepare() || Doctrine_Connection::prepare() || query ||
|| preExec() || Doctrine_Connection::exec() || query ||
|| postExec() || Doctrine_Connection::exec() || query, rows ||
|| preStmtExecute() || Doctrine_Connection_Statement::execute() || query ||
|| postStmtExecute() || Doctrine_Connection_Statement::execute() || query ||
|| preExecute() || Doctrine_Connection::execute() * || query ||
|| postExecute() || Doctrine_Connection::execute() * || query ||
|| preFetch() || Doctrine_Connection::fetch() || query, data ||
|| postFetch() || Doctrine_Connection::fetch() || query, data ||
|| preFetchAll() || Doctrine_Connection::fetchAll() || query, data ||
|| postFetchAll() || Doctrine_Connection::fetchAll() || query, data ||

* 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.


++ Query listeners
+++ preHydrate, postHydrate
+++ preBuildQuery, postBuildQuery
++ Record listeners

Doctrine_Record provides listeners very similar to Doctrine_Connection. You can set the listeners at global, connection and record(=table) level.

Here is a list of all availible listener methods:

||~ Methods     ||~ Listens ||
|| preSave() || Doctrine_Record::save() ||
|| postSave() || Doctrine_Record::save() ||
|| preUpdate() || Doctrine_Record::save() when the record state is DIRTY ||
|| postUpdate() || Doctrine_Record::save() when the record state is DIRTY ||
|| preInsert() || Doctrine_Record::save() when the record state is TDIRTY ||
|| postInsert() || Doctrine_Record::save() when the record state is TDIRTY ||
|| preDelete() || Doctrine_Record::delete() ||
|| postDelete() || Doctrine_Record::delete() ||
|| preValidate() || Doctrine_Validator::validate() ||
|| postValidate() || Doctrine_Validator::validate() ||

Just like with connection listeners there are three ways of defining a record listener: by extending Doctrine_Record_Listener, by implement Doctrine_Record_Listener_Interface or by implementing Doctrine_Overloadable. In the following we'll create a global level listener by implementing Doctrine_Overloadable:
<code type='php'>
class Logger extends Doctrine_Overloadable
{
    public function __call($m, $a) 
    {
        print 'catched event ' . $m;
        
        // do some logging here...
    }
}
</code>

Attaching the listener to manager is easy:

<code type='php'>
$manager->addRecordListener(new Logger());
</code>

Note that by adding a manager level listener it affects on all connections and all tables / records within these connections. In the following we create a connection level listener:

<code type='php'>
class Debugger extends Doctrine_Record_Listener
{
    public function preInsert(Doctrine_Event $event)
    {
        print 'inserting a record ...';
    }
    public function preUpdate(Doctrine_Event $event)
    {
        print 'updating a record...';
    }
}
</code>

Attaching the listener to a connection is as easy as:

<code type='php'>
$conn->addRecordListener(new Debugger());
</code>

Many times you want the listeners to be table specific so that they only apply on the actions on that given table. Here is an example:

<code type='php'>
class Debugger extends Doctrine_Record_Listener
{
    public function postDelete(Doctrine_Event $event)
    {
        print 'deleted ' . $event->getInvoker()->id;
    }
}
</code>

Attaching this listener to given table can be done as follows:

<code type='php'>
class MyRecord extends Doctrine_Record
{
    public function setTableDefinition()
    {
        // some definitions
    }
    
    public function setUp()
    {
        $this->addListener(new Debugger());
    }
}
</code>



++ Record hooks
||~ Methods     ||~ Listens ||
|| preSave() || Doctrine_Record::save() ||
|| postSave() || Doctrine_Record::save() ||
|| preUpdate() || Doctrine_Record::save() when the record state is DIRTY ||
|| postUpdate() || Doctrine_Record::save() when the record state is DIRTY ||
|| preInsert() || Doctrine_Record::save() when the record state is TDIRTY ||
|| postInsert() || Doctrine_Record::save() when the record state is TDIRTY ||
|| preDelete() || Doctrine_Record::delete() ||
|| postDelete() || Doctrine_Record::delete() ||
|| preValidate() || Doctrine_Validator::validate() ||
|| postValidate() || Doctrine_Validator::validate() ||

Example 1. Using insert and update hooks
<code type='php'>
class Blog extends Doctrine_Record 
{
    public function setTableDefinition()
    {
        $this->hasColumn('title', 'string', 200);
        $this->hasColumn('content', 'string');
        $this->hasColumn('created', 'date');
        $this->hasColumn('updated', 'date');
    }
    public function preInsert()
    {
        $this->created = date('Y-m-d', time());
    }
    public function preUpdate()
    {
        $this->updated = date('Y-m-d', time());
    }
}
</code>

++ Listening events
++ Chaining listeners

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>

++ The Event object
+++ Getting the invoker
You can get the object that invoked the event by calling getInvoker():
<code type='php'>
class MyListener extends Doctrine_EventListener
{
    public function preExec(Doctrine_Event $event)
    {
        $event->getInvoker(); // Doctrine_Connection
    }
}
</code>
+++ Event codes

Doctrine_Event uses constants as event codes. Above is the list of all availible event constants:

* Doctrine_Event::CONN_QUERY 
* Doctrine_Event::CONN_EXEC
* Doctrine_Event::CONN_PREPARE 
* Doctrine_Event::CONN_CONNECT 
* Doctrine_Event::STMT_EXECUTE 
* Doctrine_Event::STMT_FETCH 
* Doctrine_Event::STMT_FETCHALL

<code type='php'>
class MyListener extends Doctrine_EventListener
{
    public function preExec(Doctrine_Event $event)
    {
        $event->getCode(); // Doctrine_Event::CONN_EXEC
    }
}
</code>

* Doctrine_Event::TX_BEGIN 
* Doctrine_Event::TX_COMMIT
* Doctrine_Event::TX_ROLLBACK
* Doctrine_Event::SAVEPOINT_CREATE 
* Doctrine_Event::SAVEPOINT_ROLLBACK 
* Doctrine_Event::SAVEPOINT_COMMIT 
* Doctrine_Event::RECORD_DELETE
* Doctrine_Event::RECORD_SAVE
* Doctrine_Event::RECORD_UPDATE
* Doctrine_Event::RECORD_INSERT
* Doctrine_Event::RECORD_SERIALIZE
* Doctrine_Event::RECORD_UNSERIALIZE

<code type='php'>
class MyRecord extends Doctrine_Record
{
    public function preUpdate(Doctrine_Event $event)
    {
        $event->getCode(); // Doctrine_Event::RECORD_UPDATE
    }
}
</code>
+++ getInvoker()

The method getInvoker() returns the object that invoked the given event. For example for event Doctrine_Event::CONN_QUERY the invoker is a Doctrine_Connection object. Example:

<code type='php'>
class MyRecord extends Doctrine_Record
{
    public function preUpdate(Doctrine_Event $event)
    {
        $event->getInvoker(); // Object(MyRecord)
    }
}
</code>

+++ skipOperation()
Doctrine_Event provides many methods for altering the execution of the listened method as well as for altering the behaviour of the listener chain.

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:

<code type='php'>
class MyListener extends Doctrine_EventListener
{
    public function preExec(Doctrine_Event $event)
    {
        // some business logic, then:

        $event->skipNextListener();
    }
}
</code>

