diff --git a/lib/Doctrine/AuditLog.php b/lib/Doctrine/AuditLog.php index f24d1b57d..3f574baf5 100644 --- a/lib/Doctrine/AuditLog.php +++ b/lib/Doctrine/AuditLog.php @@ -35,7 +35,7 @@ class Doctrine_AuditLog 'className' => '%CLASS%Version', 'versionColumn' => 'version', 'generateFiles' => false, - 'table' => null, + 'table' => false, ); protected $_auditTable; @@ -43,6 +43,10 @@ class Doctrine_AuditLog public function __construct($options) { $this->_options = array_merge($this->_options, $options); + + $this->_options['className'] = str_replace('%CLASS%', + $this->_options['table']->getComponentName(), + $this->_options['className']); } /** * __get @@ -109,13 +113,13 @@ class Doctrine_AuditLog } public function getVersion(Doctrine_Record $record, $version) - { - $className = str_replace('%CLASS%', $this->_table->getComponentName(), $this->_options['className']); - + { + $className = $this->_options['className']; + $q = new Doctrine_Query(); - + $values = array(); - foreach ((array) $this->_table->getIdentifier() as $id) { + foreach ((array) $this->_options['table']->getIdentifier() as $id) { $conditions[] = $className . '.' . $id . ' = ?'; $values[] = $record->get($id); } @@ -123,9 +127,10 @@ class Doctrine_AuditLog $values[] = $version; - return $q->from($className) - ->where($where) - ->execute($values, Doctrine_HYDRATE::HYDRATE_ARRAY); + $q->from($className) + ->where($where); + + return $q->execute($values, Doctrine_HYDRATE::HYDRATE_ARRAY); } public function buildDefinition(Doctrine_Table $table) { diff --git a/lib/Doctrine/AuditLog/Listener.php b/lib/Doctrine/AuditLog/Listener.php index 6bc9506a8..ff3ed9a01 100644 --- a/lib/Doctrine/AuditLog/Listener.php +++ b/lib/Doctrine/AuditLog/Listener.php @@ -38,22 +38,31 @@ class Doctrine_AuditLog_Listener extends Doctrine_Record_Listener public function __construct(Doctrine_AuditLog $auditLog) { $this->_auditLog = $auditLog; } - public function onPreInsert(Doctrine_Record $record) + public function preInsert(Doctrine_Event $event) { $versionColumn = $this->_auditLog->getOption('versionColumn'); - $record->set($versionColumn, 1); + $event->getInvoker()->set($versionColumn, 1); } - public function onPreDelete(Doctrine_Record $record) + public function preDelete(Doctrine_Event $event) { + $class = $this->_auditLog->getOption('className'); + + $record = $event->getInvoker(); + + $version = new $class(); + $version->merge($record->toArray()); + $versionColumn = $this->_auditLog->getOption('versionColumn'); $version = $record->get($versionColumn); $record->set($versionColumn, ++$version); } - public function onPreUpdate(Doctrine_Record $record) + public function preUpdate(Doctrine_Event $event) { $versionColumn = $this->_auditLog->getOption('versionColumn'); + $record = $event->getInvoker(); + $version = $record->get($versionColumn); $record->set($versionColumn, ++$version);