From e1fbae22a429308c5100d5e4fc81ad5c1b51d40e Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 25 Jun 2007 10:08:03 +0000 Subject: [PATCH] EventListener method names updated --- lib/Doctrine/Connection.php | 48 +++-- lib/Doctrine/Connection/Profiler.php | 6 +- lib/Doctrine/Connection/Statement.php | 53 +++-- lib/Doctrine/Connection/UnitOfWork.php | 25 +-- lib/Doctrine/Event.php | 10 +- lib/Doctrine/EventListener.php | 80 +++---- lib/Doctrine/EventListener/Chain.php | 255 +++++------------------ lib/Doctrine/EventListener/Interface.php | 74 ++----- lib/Doctrine/Manager.php | 4 +- lib/Doctrine/Record.php | 15 -- lib/Doctrine/Transaction.php | 89 ++++---- tests/Connection/ProfilerTestCase.php | 29 +-- tests/DBTestCase.php | 77 +++---- tests/Record/HookTestCase.php | 52 ++++- tests/classes.php | 2 +- 15 files changed, 310 insertions(+), 509 deletions(-) diff --git a/lib/Doctrine/Connection.php b/lib/Doctrine/Connection.php index 95e4fb4ab..fb2f34be8 100644 --- a/lib/Doctrine/Connection.php +++ b/lib/Doctrine/Connection.php @@ -64,10 +64,6 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * keys representing Doctrine_Table component names and values as Doctrine_Table objects */ protected $tables = array(); - /** - * @var array $exported - */ - protected $exported = array(); /** * @var string $driverName the name of this connection driver */ @@ -331,7 +327,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $event = new Doctrine_Event($this, Doctrine_Event::CONNECT); - $this->getListener()->onPreConnect($event); + $this->getListener()->preConnect($event); $e = explode(':', $this->options['dsn']); $found = false; @@ -365,7 +361,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $this->isConnected = true; - $this->getListener()->onConnect($event); + $this->getListener()->postConnect($event); return true; } @@ -676,13 +672,17 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun { $this->connect(); - $event = new Doctrine_Event($this, Doctrine_Db_Event::PREPARE, $statement); + $event = new Doctrine_Event($this, Doctrine_Event::PREPARE, $statement); - $this->getAttribute(Doctrine::ATTR_LISTENER)->onPrePrepare($event); + $this->getAttribute(Doctrine::ATTR_LISTENER)->prePrepare($event); - $stmt = $this->dbh->prepare($statement); + $stmt = false; - $this->getAttribute(Doctrine::ATTR_LISTENER)->onPrepare($event); + if ( ! $event->skipOperation) { + $stmt = $this->dbh->prepare($statement); + } + + $this->getAttribute(Doctrine::ATTR_LISTENER)->postPrepare($event); return new Doctrine_Connection_Statement($this, $stmt); } @@ -750,7 +750,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun * * @return PDOStatement|Doctrine_Adapter_Statement */ - public function execute($query, array $params = array()) + public function execute($query, array $params = array()) { $this->connect(); @@ -760,15 +760,16 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun $stmt->execute($params); return $stmt; } else { - $event = new Doctrine_Event($this, Doctrine_EVENT::QUERY, $query, $params); + $event = new Doctrine_Event($this, Doctrine_Event::QUERY, $query, $params); - $this->getAttribute(Doctrine::ATTR_LISTENER)->onPreQuery($event); + $this->getAttribute(Doctrine::ATTR_LISTENER)->preQuery($event); - $stmt = $this->dbh->query($query); - - $this->getAttribute(Doctrine::ATTR_LISTENER)->onQuery($event); - - $this->_count++; + if ( ! $event->skipOperation) { + $stmt = $this->dbh->query($query); + + $this->_count++; + } + $this->getAttribute(Doctrine::ATTR_LISTENER)->postQuery($event); return $stmt; } @@ -796,13 +797,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun } else { $event = new Doctrine_Event($this, Doctrine_EVENT::EXEC, $query, $params); - $this->getAttribute(Doctrine::ATTR_LISTENER)->onPreExec($event); + $this->getAttribute(Doctrine::ATTR_LISTENER)->preExec($event); - $count = $this->dbh->exec($query); + if ( ! $event->skipOperation) { + $count = $this->dbh->exec($query); - $this->getAttribute(Doctrine::ATTR_LISTENER)->onExec($event); - - $this->_count++; + $this->_count++; + } + $this->getAttribute(Doctrine::ATTR_LISTENER)->postExec($event); return $count; } diff --git a/lib/Doctrine/Connection/Profiler.php b/lib/Doctrine/Connection/Profiler.php index 1b51fa1a9..aafd772cf 100644 --- a/lib/Doctrine/Connection/Profiler.php +++ b/lib/Doctrine/Connection/Profiler.php @@ -80,12 +80,8 @@ class Doctrine_Connection_Profiler implements Doctrine_Overloadable, IteratorAgg throw new Doctrine_Connection_Profiler_Exception("Couldn't listen event. Event should be an instance of Doctrine_Event."); } - // event methods should start with 'on' - if (substr($m, 0, 2) !== 'on') { - throw new Doctrine_Connection_Profiler_Exception("Couldn't invoke listener :" . $m); - } - if (substr($m, 2, 3) === 'Pre' && substr($m, 2, 7) !== 'Prepare') { + if (substr($m, 0, 3) === 'pre') { // pre-event listener found $a[0]->start(); diff --git a/lib/Doctrine/Connection/Statement.php b/lib/Doctrine/Connection/Statement.php index f9e828255..6be4a3606 100644 --- a/lib/Doctrine/Connection/Statement.php +++ b/lib/Doctrine/Connection/Statement.php @@ -37,10 +37,10 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf * statement holds an instance of Doctrine_Connection */ protected $_conn; - + /** + * @var mixed $_stmt PDOStatement object, boolean false or Doctrine_Adapter_Statement object + */ protected $_stmt; - - protected $_executed = false; /** * constructor * @@ -54,7 +54,7 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf $this->_stmt = $stmt; if ($stmt === false) { - throw new Doctrine_Db_Exception('Unknown statement object given.'); + throw new Doctrine_Exception('Unknown statement object given.'); } } /** @@ -215,14 +215,14 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf { $event = new Doctrine_Event($this, Doctrine_Event::EXECUTE, $this->_stmt->queryString, $params); // print $this->_stmt->queryString . print_r($params, true) . "
"; - $skip = $this->_conn->getListener()->onPreExecute($event); + $this->_conn->getListener()->preExecute($event); - if ( ! $skip) { + if ( ! $event->skipOperation) { $this->_stmt->execute($params); $this->_conn->incrementQueryCount(); } - $this->_conn->getListener()->onExecute($event); + $this->_conn->getListener()->postExecute($event); return $this; } @@ -253,20 +253,23 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf * * @return mixed */ - public function fetch($fetchStyle = Doctrine::FETCH_BOTH, + public function fetch($fetchMode = Doctrine::FETCH_BOTH, $cursorOrientation = Doctrine::FETCH_ORI_NEXT, $cursorOffset = null) { - $event = new Doctrine_Db_Event($this, Doctrine_Db_Event::FETCHALL, $this->_stmt->getQuery(), - array($fetchStyle, $cursorOrientation, $cursorOffset)); + $event = new Doctrine_Db_Event($this, Doctrine_Event::FETCHALL, $this->_stmt->getQuery()); - $data = $this->_conn->getListener()->onPreFetch($event); + $event->fetchMode = $fetchMode; + $event->cursorOrientation = $cursorOrientation; + $event->cursorOffset = $cursorOffset; - if ($data === null) { - $data = $this->_stmt->fetch($fetchStyle, $cursorOrientation, $cursorOffset); + $data = $this->_conn->getListener()->preFetch($event); + + if ( ! $event->skipOperation) { + $data = $this->_stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset); } - $this->_conn->getListener()->onFetch($event); + $this->_conn->getListener()->postFetch($event); return $data; } @@ -274,29 +277,35 @@ class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interf * fetchAll * Returns an array containing all of the result set rows * - * + * @param integer $fetchMode Controls how the next row will be returned to the caller. + * This value must be one of the Doctrine::FETCH_* constants, + * defaulting to Doctrine::FETCH_BOTH * * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is * Doctrine::FETCH_COLUMN. Defaults to 0. * * @return array */ - public function fetchAll($fetchStyle = Doctrine::FETCH_BOTH, + public function fetchAll($fetchMode = Doctrine::FETCH_BOTH, $columnIndex = null) { - $event = new Doctrine_Db_Event($this, Doctrine_Db_Event::FETCHALL, $this->_stmt->getQuery(), array($fetchStyle, $columnIndex)); + $event = new Doctrine_Db_Event($this, Doctrine_Event::FETCHALL, $this->_stmt->getQuery()); + $event->fetchMode = $fetchMode; + $event->columnIndex = $columnIndex; - $data = $this->_conn->getListener()->onPreFetchAll($event); + $this->_conn->getListener()->preFetchAll($event); - if ($data === null) { + if ( ! $event->skipOperation) { if ($columnIndex !== null) { - $data = $this->_stmt->fetchAll($fetchStyle, $columnIndex); + $data = $this->_stmt->fetchAll($fetchMode, $columnIndex); } else { - $data = $this->_stmt->fetchAll($fetchStyle); + $data = $this->_stmt->fetchAll($fetchMode); } + + $event->data = $data; } - $this->_conn->getListener()->onFetchAll($event); + $this->_conn->getListener()->postFetchAll($event); return $data; } diff --git a/lib/Doctrine/Connection/UnitOfWork.php b/lib/Doctrine/Connection/UnitOfWork.php index a95680800..098b28e3d 100644 --- a/lib/Doctrine/Connection/UnitOfWork.php +++ b/lib/Doctrine/Connection/UnitOfWork.php @@ -139,13 +139,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module */ public function save(Doctrine_Record $record) { - $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreSave($record); - $event = new Doctrine_Event($this, Doctrine_Event::SAVE); $record->preSave($event); - if ( ! $event->getOption('skipOperation')) { + if ( ! $event->skipOperation) { switch ($record->state()) { case Doctrine_Record::STATE_TDIRTY: $this->insert($record); @@ -162,8 +160,6 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module } $record->postSave($event); - - $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onSave($record); } /** * deletes this data access object and all the related composites @@ -180,21 +176,17 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module } $this->conn->beginTransaction(); - $record->getTable()->getListener()->onPreDelete($record); - $event = new Doctrine_Event($this, Doctrine_Event::DELETE); $record->preDelete($event); $this->deleteComposites($record); - if ( ! $event->getOption('skipOperation')) { + if ( ! $event->skipOperation) { $this->conn->transaction->addDelete($record); $record->state(Doctrine_Record::STATE_TCLEAN); } - $record->getTable()->getListener()->onDelete($record); - $record->postDelete($event); $this->conn->commit(); @@ -334,13 +326,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module */ public function update(Doctrine_Record $record) { - $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreUpdate($record); - $event = new Doctrine_Event($this, Doctrine_Event::UPDATE); $record->preUpdate($event); - if ( ! $event->getOption('skipOperation')) { + if ( ! $event->skipOperation) { $array = $record->getPrepared(); if (empty($array)) { @@ -380,8 +370,6 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module } $record->postUpdate($event); - $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onUpdate($record); - return true; } /** @@ -393,13 +381,11 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module public function insert(Doctrine_Record $record) { // listen the onPreInsert event - $record->getTable()->getAttribute(Doctrine::ATTR_LISTENER)->onPreInsert($record); - $event = new Doctrine_Event($this, Doctrine_Event::INSERT); $record->preInsert($event); - if ( ! $event->getOption('skipOperation')) { + if ( ! $event->skipOperation) { $array = $record->getPrepared(); if (empty($array)) { @@ -440,9 +426,6 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module } $record->postInsert($event); - // listen the onInsert event - $table->getAttribute(Doctrine::ATTR_LISTENER)->onInsert($record); - return true; } } diff --git a/lib/Doctrine/Event.php b/lib/Doctrine/Event.php index d0f8529b4..7e54f6367 100644 --- a/lib/Doctrine/Event.php +++ b/lib/Doctrine/Event.php @@ -144,7 +144,7 @@ class Doctrine_Event * @param string $option the name of the option * @return mixed */ - public function getOption($option) + public function __get($option) { if ( ! isset($this->_options[$option])) { return null; @@ -155,13 +155,15 @@ class Doctrine_Event /** * skipOperation * skips the next operation - * an alias for setOption('skipOperation', true) + * an alias for __set('skipOperation', true) * * @return Doctrine_Event this object */ public function skipOperation() { - return $this->setOption('skipOperation', true); + $this->_options['skipOperation'] = true; + + return $this; } /** * setOption @@ -171,7 +173,7 @@ class Doctrine_Event * @param mixed $value the value of the given option * @return Doctrine_Event this object */ - public function setOption($option, $value) + public function __set($option, $value) { $this->_options[$option] = $value; diff --git a/lib/Doctrine/EventListener.php b/lib/Doctrine/EventListener.php index d62d8ccbe..d6b3507ba 100644 --- a/lib/Doctrine/EventListener.php +++ b/lib/Doctrine/EventListener.php @@ -45,31 +45,6 @@ class Doctrine_EventListener implements Doctrine_EventListener_Interface public function onWakeUp(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) @@ -87,57 +62,54 @@ class Doctrine_EventListener implements Doctrine_EventListener_Interface public function onOpen(Doctrine_Connection $connection) { } - - - - - public function onTransactionCommit(Doctrine_Event $event) + public function preTransactionCommit(Doctrine_Event $event) { } - public function onPreTransactionCommit(Doctrine_Event $event) + public function postTransactionCommit(Doctrine_Event $event) { } - public function onTransactionRollback(Doctrine_Event $event) + public function preTransactionRollback(Doctrine_Event $event) { } - public function onPreTransactionRollback(Doctrine_Event $event) + public function postTransactionRollback(Doctrine_Event $event) { } - public function onTransactionBegin(Doctrine_Event $event) + public function preTransactionBegin(Doctrine_Event $event) { } - public function onPreTransactionBegin(Doctrine_Event $event) + public function postTransactionBegin(Doctrine_Event $event) { } - public function onConnect(Doctrine_Event $event) + public function postConnect(Doctrine_Event $event) { } - public function onPreConnect(Doctrine_Event $event) - { } - public function onPreQuery(Doctrine_Event $event) - { } - public function onQuery(Doctrine_Event $event) + public function preConnect(Doctrine_Event $event) { } - public function onPrePrepare(Doctrine_Event $event) + public function preQuery(Doctrine_Event $event) { } - public function onPrepare(Doctrine_Event $event) + public function postQuery(Doctrine_Event $event) { } - public function onPreExec(Doctrine_Event $event) + public function prePrepare(Doctrine_Event $event) { } - public function onExec(Doctrine_Event $event) - { } - - public function onPreFetch(Doctrine_Event $event) - { } - public function onFetch(Doctrine_Event $event) + public function postPrepare(Doctrine_Event $event) { } - public function onPreFetchAll(Doctrine_Event $event) + public function preExec(Doctrine_Event $event) { } - public function onFetchAll(Doctrine_Event $event) + public function postExec(Doctrine_Event $event) { } - public function onPreExecute(Doctrine_Event $event) + public function preFetch(Doctrine_Event $event) { } - public function onExecute(Doctrine_Event $event) + public function postFetch(Doctrine_Event $event) + { } + + public function preFetchAll(Doctrine_Event $event) + { } + public function postFetchAll(Doctrine_Event $event) + { } + + public function preExecute(Doctrine_Event $event) + { } + public function postExecute(Doctrine_Event $event) { } } diff --git a/lib/Doctrine/EventListener/Chain.php b/lib/Doctrine/EventListener/Chain.php index 20716acc9..f1fb61351 100644 --- a/lib/Doctrine/EventListener/Chain.php +++ b/lib/Doctrine/EventListener/Chain.php @@ -138,163 +138,6 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E $listener->onWakeUp($record); } } - /** - * onUpdate - * an event invoked after Doctrine_Record is updated - * - * @param Doctrine_Record $record - * @return void - */ - public function onUpdate(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onUpdate($record); - } - } - /** - * onPreUpdate - * an event invoked before Doctrine_Record is updated - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreUpdate(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreUpdate($record); - } - } - /** - * onCreate - * an event invoked when a new Doctrine_Record is created - * - * @param Doctrine_Record $record - * @return void - */ - public function onCreate(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onCreate($record); - } - } - /** - * onPreCreate - * an event invoked when a new Doctrine_Record - * but not yet initialized - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreCreate(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreCreate($record); - } - } - /** - * onSave - * an event invoked after a Doctrine_Record is saved (inserted / updated) - * - * @param Doctrine_Record $record - * @return void - */ - public function onSave(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onSave($record); - } - } - /** - * onSave - * an event invoked after a Doctrine_Record is saved (inserted / updated) - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreSave(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreSave($record); - } - } - /** - * onInsert - * an event invoked after Doctrine_Record is inserted into database - * - * @param Doctrine_Record $record - * @return void - */ - public function onInsert(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onInsert($record); - } - } - /** - * onPreInsert - * an event invoked before Doctrine_Record is inserted into database - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreInsert(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreInsert($record); - } - } - /** - * onDelete - * an event invoked after Doctrine_Record is deleted from database - * - * @param Doctrine_Record $record - * @return void - */ - public function onDelete(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onDelete($record); - } - } - /** - * onPreDelete - * an event invoked before Doctrine_Record is deleted from database - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreDelete(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreDelete($record); - } - } - /** - * onEvict - * an event invoked after Doctrine_Record is evicted from record repository - * - * @param Doctrine_Record $record - * @return void - */ - public function onEvict(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onEvict($record); - } - } - /** - * onPreEvict - * an event invoked before Doctrine_Record is evicted from record repository - * - * @param Doctrine_Record $record - * @return void - */ - public function onPreEvict(Doctrine_Record $record) - { - foreach ($this->listeners as $listener) { - $listener->onPreEvict($record); - } - } /** * onClose * an event invoked after Doctrine_Connection is closed @@ -341,10 +184,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onTransactionCommit(Doctrine_Event $event) + public function postTransactionCommit(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onTransactionCommit($event); + $listener->postTransactionCommit($event); } } /** @@ -354,10 +197,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onPreTransactionCommit(Doctrine_Event $event) + public function preTransactionCommit(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreTransactionCommit($event); + $listener->preTransactionCommit($event); } } /** @@ -367,10 +210,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onTransactionRollback(Doctrine_Event $event) + public function postTransactionRollback(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onTransactionRollback($event); + $listener->postTransactionRollback($event); } } /** @@ -380,10 +223,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onPreTransactionRollback(Doctrine_Event $event) + public function preTransactionRollback(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreTransactionRollback($event); + $listener->preTransactionRollback($event); } } /** @@ -393,10 +236,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onTransactionBegin(Doctrine_Event $event) + public function postTransactionBegin(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onTransactionBegin($event); + $listener->postTransactionBegin($event); } } /** @@ -406,10 +249,10 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E * @param Doctrine_Event $event * @return void */ - public function onPreTransactionBegin(Doctrine_Event $event) + public function preTransactionBegin(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreTransactionBegin($event); + $listener->preTransactionBegin($event); } } /** @@ -438,95 +281,95 @@ class Doctrine_EventListener_Chain extends Doctrine_Access implements Doctrine_E $listener->onPreCollectionDelete($collection); } } - public function onConnect(Doctrine_Event $event) + public function postConnect(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onConnect($event); + $listener->postConnect($event); } } - public function onPreConnect(Doctrine_Event $event) - { + public function preConnect(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onPreConnect($event); + $listener->preConnect($event); } } - public function onPreQuery(Doctrine_Event $event) + public function preQuery(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreQuery($event); + $listener->preQuery($event); } } - public function onQuery(Doctrine_Event $event) - { + public function postQuery(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onQuery($event); + $listener->postQuery($event); } } - public function onPrePrepare(Doctrine_Event $event) + public function prePrepare(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPrePrepare($event); + $listener->prePrepare($event); } } - public function onPrepare(Doctrine_Event $event) - { + public function postPrepare(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onPrepare($event); + $listener->postPrepare($event); } } - public function onPreExec(Doctrine_Event $event) - { + public function preExec(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onPreExec($event); + $listener->preExec($event); } } - public function onExec(Doctrine_Event $event) - { + public function postExec(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onExec($event); + $listener->postExec($event); } } - public function onPreFetch(Doctrine_Event $event) + public function preFetch(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreFetch($event); + $listener->preFetch($event); } } - public function onFetch(Doctrine_Event $event) - { + public function postFetch(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onFetch($event); + $listener->postFetch($event); } } - public function onPreFetchAll(Doctrine_Event $event) + public function preFetchAll(Doctrine_Event $event) { foreach ($this->listeners as $listener) { - $listener->onPreFetchAll($event); + $listener->preFetchAll($event); } } - public function onFetchAll(Doctrine_Event $event) - { + public function postFetchAll(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onFetchAll($event); + $listener->postFetchAll($event); } } - public function onPreExecute(Doctrine_Event $event) - { + public function preExecute(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onPreExecute($event); + $listener->preExecute($event); } } - public function onExecute(Doctrine_Event $event) - { + public function postExecute(Doctrine_Event $event) + { foreach ($this->listeners as $listener) { - $listener->onExecute($event); + $listener->postExecute($event); } } } diff --git a/lib/Doctrine/EventListener/Interface.php b/lib/Doctrine/EventListener/Interface.php index 539d9bf8d..39db910ab 100644 --- a/lib/Doctrine/EventListener/Interface.php +++ b/lib/Doctrine/EventListener/Interface.php @@ -34,67 +34,33 @@ Doctrine::autoload('Doctrine_EventListener_Interface'); */ interface Doctrine_EventListener_Interface { - public function onLoad(Doctrine_Record $record); - public function onPreLoad(Doctrine_Record $record); + public function preTransactionCommit(Doctrine_Event $event); + public function postTransactionCommit(Doctrine_Event $event); - public function onSleep(Doctrine_Record $record); - public function onWakeUp(Doctrine_Record $record); + public function preTransactionRollback(Doctrine_Event $event); + public function postTransactionRollback(Doctrine_Event $event); - public function onUpdate(Doctrine_Record $record); - public function onPreUpdate(Doctrine_Record $record); + public function preTransactionBegin(Doctrine_Event $event); + public function postTransactionBegin(Doctrine_Event $event); - public function onCreate(Doctrine_Record $record); - public function onPreCreate(Doctrine_Record $record); + public function postConnect(Doctrine_Event $event); + public function preConnect(Doctrine_Event $event); - public function onSave(Doctrine_Record $record); - public function onPreSave(Doctrine_Record $record); + public function preQuery(Doctrine_Event $event); + public function postQuery(Doctrine_Event $event); - public function onInsert(Doctrine_Record $record); - public function onPreInsert(Doctrine_Record $record); + public function prePrepare(Doctrine_Event $event); + public function postPrepare(Doctrine_Event $event); - public function onDelete(Doctrine_Record $record); - public function onPreDelete(Doctrine_Record $record); + public function preExec(Doctrine_Event $event); + public function postExec(Doctrine_Event $event); - public function onEvict(Doctrine_Record $record); - public function onPreEvict(Doctrine_Record $record); + public function preFetch(Doctrine_Event $event); + public function postFetch(Doctrine_Event $event); - public function onClose(Doctrine_Event $event); - public function onPreClose(Doctrine_Event $event); + public function preFetchAll(Doctrine_Event $event); + public function postFetchAll(Doctrine_Event $event); - public function onCollectionDelete(Doctrine_Collection $collection); - public function onPreCollectionDelete(Doctrine_Collection $collection); - - public function onOpen(Doctrine_Connection $connection); - - - - public function onConnect(Doctrine_Event $event); - public function onPreConnect(Doctrine_Event $event); - - public function onTransactionCommit(Doctrine_Event $event); - public function onPreTransactionCommit(Doctrine_Event $event); - - public function onTransactionRollback(Doctrine_Event $event); - public function onPreTransactionRollback(Doctrine_Event $event); - - public function onTransactionBegin(Doctrine_Event $event); - public function onPreTransactionBegin(Doctrine_Event $event); - - public function onPreQuery(Doctrine_Event $event); - public function onQuery(Doctrine_Event $event); - - public function onPrePrepare(Doctrine_Event $event); - public function onPrepare(Doctrine_Event $event); - - public function onPreExec(Doctrine_Event $event); - public function onExec(Doctrine_Event $event); - - public function onPreFetch(Doctrine_Event $event); - public function onFetch(Doctrine_Event $event); - - public function onPreFetchAll(Doctrine_Event $event); - public function onFetchAll(Doctrine_Event $event); - - public function onPreExecute(Doctrine_Event $event); - public function onExecute(Doctrine_Event $event); + public function preExecute(Doctrine_Event $event); + public function postExecute(Doctrine_Event $event); } diff --git a/lib/Doctrine/Manager.php b/lib/Doctrine/Manager.php index d59053129..ad85c5521 100644 --- a/lib/Doctrine/Manager.php +++ b/lib/Doctrine/Manager.php @@ -322,7 +322,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera case 'mock': case 'oracle': if ( ! isset($parts['path']) || $parts['path'] == '/') { - throw new Doctrine_Db_Exception('No database availible in data source name'); + throw new Doctrine_Manager_Exception('No database availible in data source name'); } if (isset($parts['path'])) { $parts['database'] = substr($parts['path'], 1); @@ -345,7 +345,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera } break; default: - throw new Doctrine_Db_Exception('Unknown driver '.$parts['scheme']); + throw new Doctrine_Manager_Exception('Unknown driver '.$parts['scheme']); } diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index acbf3180e..e636dae32 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -157,14 +157,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $keys = $this->_table->getPrimaryKeys(); - if ( ! $exists) { - // listen the onPreCreate event - $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onPreCreate($this); - } else { - - // listen the onPreLoad event - $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onPreLoad($this); - } // get the data array $this->_data = $this->_table->getData(); @@ -185,19 +177,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite // set the default values for this record $this->assignDefaultValues(); - - // listen the onCreate event - $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onCreate($this); - } else { $this->_state = Doctrine_Record::STATE_CLEAN; if ($count < $this->_table->getColumnCount()) { $this->_state = Doctrine_Record::STATE_PROXY; } - - // listen the onLoad event - $this->_table->getAttribute(Doctrine::ATTR_LISTENER)->onLoad($this); } $this->_errorStack = new Doctrine_Validator_ErrorStack(); diff --git a/lib/Doctrine/Transaction.php b/lib/Doctrine/Transaction.php index 2e2abc94d..e08caa34a 100644 --- a/lib/Doctrine/Transaction.php +++ b/lib/Doctrine/Transaction.php @@ -224,7 +224,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module if ($this->transactionLevel == 0) { $event = new Doctrine_Event($this, Doctrine_Event::BEGIN); - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionBegin($event); + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionBegin($event); try { $this->conn->getDbh()->beginTransaction(); @@ -232,7 +232,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module throw new Doctrine_Transaction_Exception($e->getMessage()); } - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionBegin($event); + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->postTransactionBegin($event); } } @@ -246,7 +246,7 @@ class Doctrine_Transaction extends Doctrine_Connection_Module * progress or release a savepoint. This function may only be called when * auto-committing is disabled, otherwise it will fail. * - * Listeners: onPreTransactionCommit, onTransactionCommit + * Listeners: preTransactionCommit, postTransactionCommit * * @param string $savepoint name of a savepoint to release * @throws Doctrine_Transaction_Exception if the transaction fails at database level @@ -268,36 +268,39 @@ class Doctrine_Transaction extends Doctrine_Connection_Module } else { if ($this->transactionLevel == 1) { $event = new Doctrine_Event($this, Doctrine_Event::COMMIT); - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionCommit($event); + + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionCommit($event); - try { - $this->bulkDelete(); - - } catch(Exception $e) { - $this->rollback(); - - throw new Doctrine_Transaction_Exception($e->getMessage()); - } - if ( ! empty($this->invalid)) { - $this->rollback(); - - $tmp = $this->invalid; - $this->invalid = array(); - - throw new Doctrine_Validator_Exception($tmp); + if ( ! $event->skipOperation) { + try { + $this->bulkDelete(); + + } catch(Exception $e) { + $this->rollback(); + + throw new Doctrine_Transaction_Exception($e->getMessage()); + } + if ( ! empty($this->invalid)) { + $this->rollback(); + + $tmp = $this->invalid; + $this->invalid = array(); + + throw new Doctrine_Validator_Exception($tmp); + } + + // take snapshots of all collections used within this transaction + foreach (array_unique($this->_collections) as $coll) { + $coll->takeSnapshot(); + } + $this->_collections = array(); + + $this->conn->getDbh()->commit(); + + //$this->conn->unitOfWork->reset(); } - // take snapshots of all collections used within this transaction - foreach (array_unique($this->_collections) as $coll) { - $coll->takeSnapshot(); - } - $this->_collections = array(); - - $this->conn->getDbh()->commit(); - - //$this->conn->unitOfWork->reset(); - - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionCommit($event); + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->postTransactionCommit($event); } } @@ -327,26 +330,28 @@ class Doctrine_Transaction extends Doctrine_Connection_Module return false; } - $event = new Doctrine_Event($this, Doctrine_Event::ROLLBACK); - - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onPreTransactionRollback($event); - if ( ! is_null($savepoint)) { $this->transactionLevel = $this->removeSavePoints($savepoint); $this->rollbackSavePoint($savepoint); } else { - //$this->conn->unitOfWork->reset(); - $this->deteles = array(); + $event = new Doctrine_Event($this, Doctrine_Event::ROLLBACK); + + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->preTransactionRollback($event); + + if ( ! $event->skipOperation) { + $this->deteles = array(); - $this->transactionLevel = 0; - try { - $this->conn->getDbh()->rollback(); - } catch (Exception $e) { - throw new Doctrine_Transaction_Exception($e->getMessage()); + $this->transactionLevel = 0; + try { + $this->conn->getDbh()->rollback(); + } catch (Exception $e) { + throw new Doctrine_Transaction_Exception($e->getMessage()); + } } + + $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->postTransactionRollback($event); } - $this->conn->getAttribute(Doctrine::ATTR_LISTENER)->onTransactionRollback($event); return true; } diff --git a/tests/Connection/ProfilerTestCase.php b/tests/Connection/ProfilerTestCase.php index 124d1041b..0b9a0aaff 100644 --- a/tests/Connection/ProfilerTestCase.php +++ b/tests/Connection/ProfilerTestCase.php @@ -39,6 +39,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase {} public function setUp() {} + public function testQuery() { $this->conn = Doctrine_Manager::getInstance()->openConnection(array('sqlite::memory:')); @@ -51,12 +52,12 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'CREATE TABLE test (id INT)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXEC); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXEC); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->conn->count(), 1); } - /** + public function testPrepareAndExecute() { @@ -65,32 +66,32 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($event->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $stmt->execute(array(1)); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->conn->count(), 2); } - */ + public function testMultiplePrepareAndExecute() { $stmt = $this->conn->prepare('INSERT INTO test (id) VALUES (?)'); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $stmt2 = $this->conn->prepare('INSERT INTO test (id) VALUES (?)'); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::PREPARE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::PREPARE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); /** TODO: strange errors here $stmt->execute(array(1)); @@ -98,7 +99,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->conn->count(), 4); @@ -117,12 +118,12 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); $this->assertEqual($this->profiler->lastEvent()->getQuery(), 'INSERT INTO test (id) VALUES (?)'); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::EXECUTE); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::EXECUTE); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } public function testTransactionRollback() @@ -135,7 +136,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::BEGIN); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); try { @@ -147,7 +148,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::ROLLBACK); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::ROLLBACK); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } public function testTransactionCommit() @@ -160,7 +161,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase } $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::BEGIN); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::BEGIN); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); try { @@ -173,7 +174,7 @@ class Doctrine_Connection_Profiler_TestCase extends Doctrine_UnitTestCase $this->assertEqual($this->profiler->lastEvent()->getQuery(), null); $this->assertTrue($this->profiler->lastEvent()->hasEnded()); - $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Db_Event::COMMIT); + $this->assertEqual($this->profiler->lastEvent()->getCode(), Doctrine_Event::COMMIT); $this->assertTrue(is_numeric($this->profiler->lastEvent()->getElapsedSecs())); } } diff --git a/tests/DBTestCase.php b/tests/DBTestCase.php index f0ecf10d0..adda7ba95 100644 --- a/tests/DBTestCase.php +++ b/tests/DBTestCase.php @@ -110,33 +110,33 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase $listener = $this->dbh->getListener(); $stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)'); - $this->assertEqual($listener->pop(), 'onPrepare'); - $this->assertEqual($listener->pop(), 'onPrePrepare'); + $this->assertEqual($listener->pop(), 'postPrepare'); + $this->assertEqual($listener->pop(), 'prePrepare'); $stmt->execute(array(1)); - $this->assertEqual($listener->pop(), 'onExecute'); - $this->assertEqual($listener->pop(), 'onPreExecute'); + $this->assertEqual($listener->pop(), 'postExecute'); + $this->assertEqual($listener->pop(), 'preExecute'); $this->dbh->exec('DELETE FROM entity'); - $this->assertEqual($listener->pop(), 'onExec'); - $this->assertEqual($listener->pop(), 'onPreExec'); + $this->assertEqual($listener->pop(), 'postExec'); + $this->assertEqual($listener->pop(), 'preExec'); $this->dbh->beginTransaction(); - $this->assertEqual($listener->pop(), 'onTransactionBegin'); - $this->assertEqual($listener->pop(), 'onPreTransactionBegin'); + $this->assertEqual($listener->pop(), 'postTransactionBegin'); + $this->assertEqual($listener->pop(), 'preTransactionBegin'); $this->dbh->exec('INSERT INTO entity (id) VALUES (1)'); - $this->assertEqual($listener->pop(), 'onExec'); - $this->assertEqual($listener->pop(), 'onPreExec'); + $this->assertEqual($listener->pop(), 'postExec'); + $this->assertEqual($listener->pop(), 'preExec'); $this->dbh->commit(); - $this->assertEqual($listener->pop(), 'onTransactionCommit'); - $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + $this->assertEqual($listener->pop(), 'postTransactionCommit'); + $this->assertEqual($listener->pop(), 'preTransactionCommit'); @@ -152,63 +152,66 @@ class Doctrine_Db_TestCase extends Doctrine_UnitTestCase $listener = $this->dbh->getListener()->get(0); $listener2 = $this->dbh->getListener()->get(1); - $this->assertEqual($listener->pop(), 'onExec'); - $this->assertEqual($listener->pop(), 'onPreExec'); + $this->assertEqual($listener->pop(), 'postExec'); + $this->assertEqual($listener->pop(), 'preExec'); - $this->assertEqual($listener2->pop(), 'onExec'); - $this->assertEqual($listener2->pop(), 'onPreExec'); + $this->assertEqual($listener2->pop(), 'postExec'); + $this->assertEqual($listener2->pop(), 'preExec'); } + public function testListeningPrepareEventsWithListenerChain() { $stmt = $this->dbh->prepare('INSERT INTO entity (id) VALUES(?)'); $listener = $this->dbh->getListener()->get(0); $listener2 = $this->dbh->getListener()->get(1); - $this->assertEqual($listener->pop(), 'onPrepare'); - $this->assertEqual($listener->pop(), 'onPrePrepare'); + $this->assertEqual($listener->pop(), 'postPrepare'); + $this->assertEqual($listener->pop(), 'prePrepare'); - $this->assertEqual($listener2->pop(), 'onPrepare'); - $this->assertEqual($listener2->pop(), 'onPrePrepare'); + $this->assertEqual($listener2->pop(), 'postPrepare'); + $this->assertEqual($listener2->pop(), 'prePrepare'); $stmt->execute(array(1)); - $this->assertEqual($listener->pop(), 'onExecute'); - $this->assertEqual($listener->pop(), 'onPreExecute'); + $this->assertEqual($listener->pop(), 'postExecute'); + $this->assertEqual($listener->pop(), 'preExecute'); - $this->assertEqual($listener2->pop(), 'onExecute'); - $this->assertEqual($listener2->pop(), 'onPreExecute'); + $this->assertEqual($listener2->pop(), 'postExecute'); + $this->assertEqual($listener2->pop(), 'preExecute'); } - public function testListeningExecEventsWithListenerChain() + + public function testListeningExecEventsWithListenerChain() { $this->dbh->exec('DELETE FROM entity'); $listener = $this->dbh->getListener()->get(0); $listener2 = $this->dbh->getListener()->get(1); - $this->assertEqual($listener->pop(), 'onExec'); - $this->assertEqual($listener->pop(), 'onPreExec'); + $this->assertEqual($listener->pop(), 'postExec'); + $this->assertEqual($listener->pop(), 'preExec'); - $this->assertEqual($listener2->pop(), 'onExec'); - $this->assertEqual($listener2->pop(), 'onPreExec'); + $this->assertEqual($listener2->pop(), 'postExec'); + $this->assertEqual($listener2->pop(), 'preExec'); } + public function testListeningTransactionEventsWithListenerChain() { $this->dbh->beginTransaction(); $listener = $this->dbh->getListener()->get(0); $listener2 = $this->dbh->getListener()->get(1); - $this->assertEqual($listener->pop(), 'onTransactionBegin'); - $this->assertEqual($listener->pop(), 'onPreTransactionBegin'); + $this->assertEqual($listener->pop(), 'postTransactionBegin'); + $this->assertEqual($listener->pop(), 'preTransactionBegin'); - $this->assertEqual($listener2->pop(), 'onTransactionBegin'); - $this->assertEqual($listener2->pop(), 'onPreTransactionBegin'); + $this->assertEqual($listener2->pop(), 'postTransactionBegin'); + $this->assertEqual($listener2->pop(), 'preTransactionBegin'); $this->dbh->exec('INSERT INTO entity (id) VALUES (1)'); $this->dbh->commit(); - $this->assertEqual($listener->pop(), 'onTransactionCommit'); - $this->assertEqual($listener->pop(), 'onPreTransactionCommit'); + $this->assertEqual($listener->pop(), 'postTransactionCommit'); + $this->assertEqual($listener->pop(), 'preTransactionCommit'); - $this->assertEqual($listener->pop(), 'onExec'); - $this->assertEqual($listener->pop(), 'onPreExec'); + $this->assertEqual($listener->pop(), 'postExec'); + $this->assertEqual($listener->pop(), 'preExec'); $this->dbh->exec('DROP TABLE entity'); } diff --git a/tests/Record/HookTestCase.php b/tests/Record/HookTestCase.php index 9a9139059..7b8bad6e6 100644 --- a/tests/Record/HookTestCase.php +++ b/tests/Record/HookTestCase.php @@ -36,7 +36,7 @@ class Doctrine_Record_Hook_TestCase extends Doctrine_UnitTestCase { } public function prepareTables() { - $this->tables = array('RecordHookTest'); + $this->tables = array('RecordHookTest', 'SoftDeleteTest'); parent::prepareTables(); } @@ -78,7 +78,41 @@ class Doctrine_Record_Hook_TestCase extends Doctrine_UnitTestCase $this->assertEqual($r->pop(), 'postDelete'); $this->assertEqual($r->pop(), 'preDelete'); } + + public function testSoftDelete() + { + $r = new SoftDeleteTest(); + $r->name = 'something'; + $r->save(); + + $this->assertEqual($r->name, 'something'); + $this->assertEqual($r->deleted, null); + $this->assertEqual($r->getState(), Doctrine_Record::STATE_CLEAN); + + $r->delete(); + + $this->assertEqual($r->getState(), Doctrine_Record::STATE_CLEAN); + $this->assertEqual($r->deleted, true); + } } +class SoftDeleteTest extends Doctrine_Record +{ + public function setTableDefinition() + { + $this->hasColumn('name', 'string', null, array('primary' => true)); + $this->hasColumn('deleted', 'boolean', 1); + } + public function preDelete($event) + { + $event->skipOperation(); + } + public function postDelete($event) + { + $this->deleted = true; + $this->save(); + } +} + class RecordHookTest extends Doctrine_Record { protected $_messages = array(); @@ -87,35 +121,35 @@ class RecordHookTest extends Doctrine_Record { $this->hasColumn('name', 'string', null, array('primary' => true)); } - public function preSave() + public function preSave($event) { $this->_messages[] = __FUNCTION__; } - public function postSave() + public function postSave($event) { $this->_messages[] = __FUNCTION__; } - public function preInsert() + public function preInsert($event) { $this->_messages[] = __FUNCTION__; } - public function postInsert() + public function postInsert($event) { $this->_messages[] = __FUNCTION__; } - public function preUpdate() + public function preUpdate($event) { $this->_messages[] = __FUNCTION__; } - public function postUpdate() + public function postUpdate($event) { $this->_messages[] = __FUNCTION__; } - public function preDelete() + public function preDelete($event) { $this->_messages[] = __FUNCTION__; } - public function postDelete() + public function postDelete($event) { $this->_messages[] = __FUNCTION__; } diff --git a/tests/classes.php b/tests/classes.php index a96e7170f..36382a85a 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -21,7 +21,7 @@ class Entity extends Doctrine_Record $this->hasColumn('created', 'integer',11); $this->hasColumn('updated', 'integer',11); $this->hasColumn('email_id', 'integer'); - $this->option('subclasses',array('User','Group')); + $this->option('subclasses', array('User','Group')); } } class FieldNameTest extends Doctrine_Record