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:
@@ -240,6 +327,19 @@ class MyRecord extends Doctrine_Record
...
@@ -240,6 +327,19 @@ class MyRecord extends Doctrine_Record
}
}
}
}
</code>
</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()
+++ 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.
Doctrine_Event provides many methods for altering the execution of the listened method as well as for altering the behaviour of the listener chain.