Changeset 4503
- Timestamp:
- 06/09/08 00:17:53 (13 months ago)
- Location:
- branches/0.11
- Files:
-
- 6 modified
-
lib/Doctrine/AuditLog.php (modified) (5 diffs)
-
lib/Doctrine/AuditLog/Listener.php (modified) (2 diffs)
-
lib/Doctrine/Record.php (modified) (1 diff)
-
lib/Doctrine/Template/Versionable.php (modified) (2 diffs)
-
tests/AuditLogTestCase.php (modified) (5 diffs)
-
tests/models/VersioningTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11/lib/Doctrine/AuditLog.php
r4252 r4503 41 41 'className' => '%CLASS%Version', 42 42 'versionColumn' => 'version', 43 'tableName' => false, 43 44 'generateFiles' => false, 44 45 'table' => false, … … 50 51 /** 51 52 * Accepts array of options to configure the AuditLog 52 * 53 * 53 54 * @param array $options An array of options 54 55 * @return void … … 60 61 61 62 /** 62 * Get array of information for the passed record and the specified version63 *64 * @param Doctrine_Record $record65 * @param mixed $version66 * @return array An array with version information67 */68 public function getVersion(Doctrine_Record $record, $version)69 {70 if ( ! $this->_options['auditLog']) {71 throw new Doctrine_Exception('Audit log is turned off, no version history is recorded.');72 }73 74 $className = $this->_options['className'];75 76 $q = new Doctrine_Query();77 78 $values = array();79 foreach ((array) $this->_options['table']->getIdentifier() as $id) {80 $conditions[] = $className . '.' . $id . ' = ?';81 $values[] = $record->get($id);82 }83 84 $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?';85 86 $values[] = $version;87 88 $q->from($className)89 ->where($where);90 91 return $q->execute($values, Doctrine::HYDRATE_ARRAY);92 }93 94 /**95 63 * Build definition for audit log table 96 * 97 * @param Doctrine_Table $table 64 * 65 * @param Doctrine_Table $table 98 66 * @return boolean true on success otherwise false. 99 67 */ … … 101 69 { 102 70 $name = $this->_options['table']->getComponentName(); 103 104 71 $columns = $this->_options['table']->getColumns(); 105 72 … … 116 83 $this->hasColumn($this->_options['versionColumn'], 'integer', 8, array('primary' => true)); 117 84 } 85 86 /** 87 * Get array of information for the passed record and the specified version 88 * 89 * @param Doctrine_Record $record 90 * @param mixed $version 91 * @return array An array with version information 92 */ 93 public function getVersion(Doctrine_Record $record, $version) 94 { 95 $className = $this->_options['className']; 96 97 $q = new Doctrine_Query(); 98 99 $values = array(); 100 foreach ((array) $this->_options['table']->getIdentifier() as $id) { 101 $conditions[] = $className . '.' . $id . ' = ?'; 102 $values[] = $record->get($id); 103 } 104 105 $where = implode(' AND ', $conditions) . ' AND ' . $className . '.' . $this->_options['versionColumn'] . ' = ?'; 106 107 $values[] = $version; 108 109 $q->from($className) 110 ->where($where); 111 112 return $q->execute($values, Doctrine::HYDRATE_ARRAY); 113 } 114 115 /** 116 * Get the highest version number for a given Doctrine_Record 117 * 118 * @param Doctrine_Record $record 119 * @return Integer $versionnumber 120 */ 121 public function getMaxVersion(Doctrine_Record $record) 122 { 123 $className = $this->_options['className']; 124 $select = 'MAX(' . $className . '.' . $this->_options['versionColumn'] . ') max_version'; 125 126 foreach ((array) $this->_options['table']->getIdentifier() as $id) { 127 $conditions[] = $className . '.' . $id . ' = ?'; 128 $values[] = $record->get($id); 129 } 130 131 $q = Doctrine_Query::create() 132 ->select($select) 133 ->from($className) 134 ->where(implode(' AND ',$conditions)); 135 136 $result = $q->execute($values, Doctrine::HYDRATE_ARRAY); 137 138 return isset($result[0]['max_version']) ? $result[0]['max_version']:0; 139 } 118 140 } -
branches/0.11/lib/Doctrine/AuditLog/Listener.php
r4252 r4503 91 91 { 92 92 if ($this->_auditLog->getOption('auditLog')) { 93 $class = $this->_auditLog->getOption('className'); 93 $className = $this->_auditLog->getOption('className'); 94 $versionColumn = $this->_auditLog->getOption('versionColumn'); 95 $event->getInvoker()->set($versionColumn, null); 94 96 95 $record = $event->getInvoker(); 97 $q = Doctrine_Query::create(); 98 foreach ((array) $this->_auditLog->getOption('table')->getIdentifier() as $id) { 99 $conditions[] = 'obj.' . $id . ' = ?'; 100 $values[] = $event->getInvoker()->get($id); 101 } 102 103 $rows = $q->delete($className) 104 ->from($className.' obj') 105 ->where(implode(' AND ',$conditions)) 106 ->execute($values); 107 108 if ( ! count($rows)){ 109 throw new Doctrine_Record_Exception('Can not delete Versions!',Doctrine::ERR_CANNOT_DELETE); 110 } 111 } 112 } 113 114 /** 115 * Pre update event hook for inserting new version record 116 * 117 * @param Doctrine_Event $event 118 * @return void 119 */ 120 public function preUpdate(Doctrine_Event $event) 121 { 122 if ($this->_auditLog->getOption('auditLog')) { 123 $class = $this->_auditLog->getOption('className'); 124 $record = $event->getInvoker(); 96 125 97 126 $versionColumn = $this->_auditLog->getOption('versionColumn'); 98 $version = $record->get($versionColumn);99 127 100 $record->set($versionColumn, ++$version);128 $record->set($versionColumn, $this->_getNextVersion($record)); 101 129 102 130 $version = new $class(); … … 107 135 108 136 /** 109 * Pre update event hook for inserting new version record137 * Get the next version for the audit log 110 138 * 111 * @param Doctrine_Event $event112 * @return void139 * @param Doctrine_Record $record 140 * @return integer $nextVersion 113 141 */ 114 p ublic function preUpdate(Doctrine_Event $event)142 protected function _getNextVersion(Doctrine_Record $record) 115 143 { 116 if ($this->_auditLog->getOption('auditLog')) { 117 $class = $this->_auditLog->getOption('className'); 118 $record = $event->getInvoker(); 119 120 $versionColumn = $this->_auditLog->getOption('versionColumn'); 121 122 $version = $record->get($versionColumn); 123 124 $record->set($versionColumn, ++$version); 125 126 $version = new $class(); 127 $version->merge($record->toArray()); 128 $version->save(); 129 } 144 if ($this->_auditLog->getOption('auditLog')) { 145 return ($this->_auditLog->getMaxVersion($record) + 1); 146 } 130 147 } 131 148 } -
branches/0.11/lib/Doctrine/Record.php
r4487 r4503 1680 1680 return $this->_node; 1681 1681 } 1682 /**1683 * revert1684 * reverts this record to given version, this method only works if versioning plugin1685 * is enabled1686 *1687 * @throws Doctrine_Record_Exception if given version does not exist1688 * @param integer $version an integer > 11689 * @return Doctrine_Record this object1690 */1691 public function revert($version)1692 {1693 $auditLog = $this->_table->getTemplate('Doctrine_Template_Versionable')->getAuditLog();1694 1695 if ( ! $auditLog->getOption('auditLog')) {1696 throw new Doctrine_Record_Exception('Audit log is turned off, no version history is recorded.');1697 }1698 1699 $data = $auditLog->getVersion($this, $version);1700 1701 if ( ! isset($data[0])) {1702 throw new Doctrine_Record_Exception('Version ' . $version . ' does not exist!');1703 }1704 1705 $this->_data = $data[0];1706 1707 return $this;1708 }1709 1682 1710 1683 public function unshiftFilter(Doctrine_Record_Filter $filter) -
branches/0.11/lib/Doctrine/Template/Versionable.php
r4442 r4503 38 38 * __construct 39 39 * 40 * @param array $options 40 * @param array $options 41 41 * @return void 42 42 */ … … 71 71 return $this->_plugin; 72 72 } 73 74 /** 75 * revert 76 * reverts this record to given version, this method only works if versioning plugin 77 * is enabled 78 * 79 * @throws Doctrine_Record_Exception if given version does not exist 80 * @param integer $version an integer > 1 81 * @return Doctrine_Record this object 82 */ 83 public function revert($version) 84 { 85 $auditLog = $this->_plugin; 86 87 if ( ! $auditLog->getOption('auditLog')) { 88 throw new Doctrine_Record_Exception('Audit log is turned off, no version history is recorded.'); 89 } 90 91 $data = $auditLog->getVersion($this->getInvoker(), $version); 92 93 if ( ! isset($data[0])) { 94 throw new Doctrine_Record_Exception('Version ' . $version . ' does not exist!'); 95 } 96 97 $this->getInvoker()->merge($data[0]); 98 99 100 return $this->getInvoker(); 101 } 73 102 } -
branches/0.11/tests/AuditLogTestCase.php
r4232 r4503 31 31 * @version $Revision$ 32 32 */ 33 class Doctrine_AuditLog_TestCase extends Doctrine_UnitTestCase 33 class Doctrine_AuditLog_TestCase extends Doctrine_UnitTestCase 34 34 { 35 35 … … 38 38 39 39 public function prepareTables() 40 { 40 { 41 41 $this->profiler = new Doctrine_Connection_Profiler(); 42 42 $this->conn->addListener($this->profiler); 43 43 $this->tables = array('VersioningTest', 'VersioningTestVersion', 'VersioningTest2'); 44 44 45 45 parent::prepareTables(); 46 46 } … … 57 57 $entity->save(); 58 58 $this->assertEqual($entity->name, 'zYne 2'); 59 59 $this->assertEqual($entity->version, 2); 60 60 $this->conn->clear(); 61 61 62 62 $entity = $this->conn->getTable('VersioningTest')->find(1); 63 64 $this->assertEqual($entity->name, 'zYne 2');65 $this->assertEqual($entity->version, 2);66 67 $entity->delete();68 $this->assertEqual($entity->version, 3);69 70 $entity->revert(2);71 63 72 64 $this->assertEqual($entity->name, 'zYne 2'); … … 77 69 $this->assertEqual($entity->name, 'zYne'); 78 70 $this->assertEqual($entity->version, 1); 71 $entity->save(); // new Version 3 should be created 72 } 79 73 80 }81 82 74 public function testRevertThrowsExceptionForTransientRecords() 83 75 { 84 76 $entity = new VersioningTest(); 85 77 86 78 try { 87 79 $entity->revert(1); … … 102 94 $this->assertTrue($entity->version, 2); 103 95 } 96 public function testTableName() 97 { 98 $entity = new VersioningTest3(); 99 $this->assertEqual($entity->getAuditLog()->getTable()->getTableName(), 'tbl_prefix_comments_version'); 100 $this->assertEqual($entity->getAuditLog()->getTable()->getComponentName(), 'VersioningTestClass'); 101 } 102 104 103 105 104 public function testNoAuditLogThrowsExceptions() -
branches/0.11/tests/models/VersioningTest.php
r4232 r4503 25 25 } 26 26 } 27 28 class VersioningTest3 extends Doctrine_Record 29 { 30 public function setTableDefinition() 31 { 32 $this->hasColumn('name', 'string'); 33 $this->hasColumn('version', 'integer'); 34 } 35 public function setUp() 36 { 37 38 $this->actAs('Versionable', array('tableName' => 'tbl_prefix_comments_version', 39 'className' => 'VersioningTestClass')); 40 41 } 42 }