+++ Introduction


+++ Creating new listener

Creating a new listener is very easy. You can set the listener in global, connection or factory level.

<code type="php">
class MyListener extends Doctrine_EventListener {
    public function onLoad(Doctrine_Record $record) {
        print $record->getTable()->getComponentName()." just got loaded!";
    }
    public function onSave(Doctrine_Record $record) {
        print "saved data access object!";
    }
}
class MyListener2 extends Doctrine_EventListener {
    public function onPreUpdate() {
        try {
            $record->set("updated",time());
        } catch(InvalidKeyException $e) { 
        }
    }
}


// setting global listener
$manager = Doctrine_Manager::getInstance();

$manager->setAttribute(Doctrine::ATTR_LISTENER,new MyListener());

// setting connection level listener
$conn = $manager->openConnection($dbh);

$conn->setAttribute(Doctrine::ATTR_LISTENER,new MyListener2());

// setting factory level listener
$table = $conn->getTable("User");

$table->setAttribute(Doctrine::ATTR_LISTENER,new MyListener());
</code>


+++ List of events

Here is a list of availible events and their parameters: 

<code type="php">
interface Doctrine_EventListener_Interface {

    public function onLoad(Doctrine_Record $record);
    public function onPreLoad(Doctrine_Record $record);

    public function onUpdate(Doctrine_Record $record);
    public function onPreUpdate(Doctrine_Record $record);

    public function onCreate(Doctrine_Record $record);
    public function onPreCreate(Doctrine_Record $record);

    public function onSave(Doctrine_Record $record);
    public function onPreSave(Doctrine_Record $record);

    public function onInsert(Doctrine_Record $record);
    public function onPreInsert(Doctrine_Record $record);

    public function onDelete(Doctrine_Record $record);
    public function onPreDelete(Doctrine_Record $record);

    public function onEvict(Doctrine_Record $record);
    public function onPreEvict(Doctrine_Record $record);

    public function onSleep(Doctrine_Record $record);
    
    public function onWakeUp(Doctrine_Record $record);
    
    public function onClose(Doctrine_Connection $connection);
    public function onPreClose(Doctrine_Connection $connection);
    
    public function onOpen(Doctrine_Connection $connection);

    public function onTransactionCommit(Doctrine_Connection $connection);
    public function onPreTransactionCommit(Doctrine_Connection $connection);

    public function onTransactionRollback(Doctrine_Connection $connection);
    public function onPreTransactionRollback(Doctrine_Connection $connection);

    public function onTransactionBegin(Doctrine_Connection $connection);
    public function onPreTransactionBegin(Doctrine_Connection $connection);
    
    public function onCollectionDelete(Doctrine_Collection $collection);
    public function onPreCollectionDelete(Doctrine_Collection $collection);
}
</code>


+++ Listening events

<code type="php">
$table = $conn->getTable("User");

$table->setEventListener(new MyListener2());

// retrieve user whose primary key is 2
$user = $table->find(2);

$user->name = "John Locke";

// update event will be listened and current time will be assigned to the field 'updated' 
$user->save();
</code>


+++ Chaining


+++ AccessorInvoker

<code type="php">
class User { 
    public function setTableDefinition() {
        $this->hasColumn("name", "string", 200);
        $this->hasColumn("password", "string", 32);
    }
    public function setPassword($password) {
        return md5($password);
    }
    public function getName($name) {
        return strtoupper($name);
    }
}

$user = new User();

$user->name = 'someone';

print $user->name; // someone

$user->password = '123'; 

print $user->password; // 123

$user->setAttribute(Doctrine::ATTR_LISTENER, new Doctrine_EventListener_AccessorInvoker());

print $user->name; // SOMEONE

$user->password = '123';

print $user->password; // 202cb962ac59075b964b07152d234b70
</code>


+++ Creating a logger
