From 6748d89075bb4625c45546c0a45bf1d4b98eed73 Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Mon, 15 Dec 2014 16:27:33 +0100 Subject: [PATCH] Fixed #5 - Save now the correct value (initial value, instead of current) --- src/ChangeLoggerBehavior.php | 72 ++++++++++++++++++++++++++++-------- tests/ChangeLoggerTest.php | 52 +++++++++++++++++++++----- 2 files changed, 100 insertions(+), 24 deletions(-) diff --git a/src/ChangeLoggerBehavior.php b/src/ChangeLoggerBehavior.php index eef9f53..f9ff1a3 100644 --- a/src/ChangeLoggerBehavior.php +++ b/src/ChangeLoggerBehavior.php @@ -48,12 +48,12 @@ public function objectAttributes($builder) foreach ($this->getColumns() as $column) { $name = lcfirst($column->getPhpName()); $script .= " - /** - * Is used as comment for ChangeLoggerBehavior. - * - * @var string - */ - protected \${$name}ChangeComment; +/** + * Is used as comment for ChangeLoggerBehavior. + * + * @var string + */ +protected \${$name}ChangeComment; "; } } @@ -63,16 +63,24 @@ public function objectAttributes($builder) $name = lcfirst($column->getPhpName()); $script .= " - /** - * Is used as createdBy for ChangeLoggerBehavior. - * - * @var string - */ - protected \${$name}ChangeBy; +/** + * Is used as createdBy for ChangeLoggerBehavior. + * + * @var string + */ +protected \${$name}ChangeBy; "; } } + $script .= " +/** + * + * @var mixed[] + */ +protected \$changeLoggerTracker = []; +"; + return $script; } @@ -142,12 +150,18 @@ protected function appendAddVersionMethod(ObjectBuilder $builder, &$script, $col $logARClassName = $builder->getClassNameFromBuilder($builder->getNewStubObjectBuilder($logTable)); $logARQueryName = $builder->getNewStubQueryBuilder($logTable)->getFullyQualifiedClassName(); + $varName = lcfirst($column->getPhpName()); + $script .= " /** - * @return $logARClassName model instance of saved log ($logTableName) + * @return $logARClassName|false model instance of saved log ($logTableName) or false if nothing was changed. */ public function $methodName() { + if (!isset(\$this->changeLoggerTracker['$varName'])) { + return false; + } + \$log = new {$logARClassName}();"; foreach ($this->getTable()->getPrimaryKey() as $col) { @@ -156,7 +170,7 @@ public function $methodName() } $script .= " - \$log->set" . $column->getPhpName() . "(\$this->get" . $column->getPhpName() . "());"; + \$log->set" . $column->getPhpName() . "(\$this->changeLoggerTracker['$varName']);"; if ('true' === $this->getParameter('created_at')) { $createdAtColumn = $logTable->getColumn($this->getParameter('created_at_column')); @@ -190,6 +204,7 @@ public function $methodName() \$log->setVersion(\$lastVersion ? \$lastVersion->getVersion() + 1 : 1); \$log->save(); + \$this->changeLoggerTracker['$varName'] = \$this->get{$column->getPhpName()}(); return \$log; } "; @@ -215,7 +230,6 @@ public function postUpdate(ObjectBuilder $builder) return $hooks; } - /** * @param ObjectBuilder $builder * @@ -234,6 +248,34 @@ public function preSave(ObjectBuilder $builder) return $hooks; } + public function postSave(ObjectBuilder $builder) + { + $hooks = ''; + + foreach ($this->getColumns() as $column) { + $getter = 'get' . $column->getPhpName(); + $varName = lcfirst($column->getPhpName()); + $hooks .= " +\$this->changeLoggerTracker['$varName'] = \$this->{$getter}();"; + } + + return $hooks; + } + + public function postHydrate(ObjectBuilder $builder) + { + $hooks = ''; + + foreach ($this->getColumns() as $column) { + $getter = 'get' . $column->getPhpName(); + $varName = lcfirst($column->getPhpName()); + $hooks .= " +\$this->changeLoggerTracker['$varName'] = \$this->{$getter}();"; + } + + return $hooks; + } + /** * @return Column[] */ diff --git a/tests/ChangeLoggerTest.php b/tests/ChangeLoggerTest.php index 583fd06..ed34223 100644 --- a/tests/ChangeLoggerTest.php +++ b/tests/ChangeLoggerTest.php @@ -47,7 +47,10 @@ public function testSingle() $item = new \ChangeloggerBehaviorSingle(); $this->assertTrue(method_exists($item, 'addTitleVersion')); + $item->setTitle('Initial'); $item->save(); + + //initial save doesn't save a log entry $this->assertEquals(0, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count()); $item->setTitle('Teschd'); @@ -55,12 +58,13 @@ public function testSingle() $item->setTitleChangeBy('Me'); $item->save(); - //initial save saves already a log entry + //second save saves a log entry $this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count()); $changeLog = \ChangeloggerBehaviorSingleTitleLogQuery::create()->findOne(); $this->assertEquals(1, $changeLog->getVersion()); $this->assertEquals('Sohalt.', $changeLog->getLogComment()); $this->assertEquals('Me', $changeLog->getLogCreatedBy()); + $this->assertEquals('Initial', $changeLog->getTitle()); $item->setAge(2); $item->save(); @@ -69,7 +73,7 @@ public function testSingle() $this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count()); $lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne(); $this->assertEquals(1, $lastVersion->getVersion()); - $this->assertEquals('Teschd', $lastVersion->getTitle()); + $this->assertEquals('Initial', $lastVersion->getTitle()); $item->setTitle('Changed'); $item->save(); @@ -78,7 +82,31 @@ public function testSingle() $this->assertEquals(2, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count()); $lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne(); $this->assertEquals(2, $lastVersion->getVersion()); - $this->assertEquals('Changed', $lastVersion->getTitle()); + $this->assertEquals('Teschd', $lastVersion->getTitle()); + } + + public function testFromQuery() + { + \ChangeloggerBehaviorSingleQuery::create()->deleteAll(); + \ChangeloggerBehaviorSingleTitleLogQuery::create()->deleteAll(); + + $item = new \ChangeloggerBehaviorSingle(); + + $item->setTitle('Initial'); + $item->save(); + + \Map\ChangeloggerBehaviorSingleTableMap::clearInstancePool(); + + $itemRetrieved = \ChangeloggerBehaviorSingleQuery::create()->findOne(); + $this->assertNotSame($itemRetrieved, $item); + + $itemRetrieved->setTitle('New Title'); + $itemRetrieved->save(); + + $this->assertEquals(1, \ChangeloggerBehaviorSingleTitleLogQuery::create()->count()); + $lastVersion = \ChangeloggerBehaviorSingleTitleLogQuery::create()->orderByVersion('desc')->findOne(); + $this->assertEquals(1, $lastVersion->getVersion()); + $this->assertEquals('Initial', $lastVersion->getTitle()); } public function testMultiple() @@ -94,7 +122,9 @@ public function testMultiple() $this->assertTrue(method_exists($item, 'addTitleVersion')); $this->assertTrue(method_exists($item, 'addAgeVersion')); + $item->setTitle('Initial'); $item->save(); + $this->assertEquals(0, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count()); $item->setTitle('Teschd'); @@ -105,7 +135,7 @@ public function testMultiple() $this->assertEquals(1, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->findOne()->getVersion()); $lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne(); $this->assertEquals(1, $lastVersion->getVersion()); - $this->assertEquals('Teschd', $lastVersion->getTitle()); + $this->assertEquals('Initial', $lastVersion->getTitle()); $this->assertEquals(0, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count()); $item->setAge(2); @@ -115,7 +145,14 @@ public function testMultiple() $this->assertEquals(1, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count()); $lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne(); $this->assertEquals(1, $lastVersion->getVersion()); - $this->assertEquals('Teschd', $lastVersion->getTitle()); + $this->assertEquals('Initial', $lastVersion->getTitle()); + + //`age` is empty, so we have no log record + $this->assertEquals(0, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count()); + + $item->setTitle('Changed'); + $item->setAge(null); + $item->save(); //we have now additional a `age` log entry $this->assertEquals(1, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count()); @@ -123,14 +160,11 @@ public function testMultiple() $this->assertEquals(1, $lastVersion->getVersion()); $this->assertEquals(2, $lastVersion->getAge()); - $item->setTitle('Changed'); - $item->save(); - //title has been changed, we have now two versions $this->assertEquals(2, \ChangeloggerBehaviorMultipleTitleLogQuery::create()->count()); $lastVersion = \ChangeloggerBehaviorMultipleTitleLogQuery::create()->orderByVersion('desc')->findOne(); $this->assertEquals(2, $lastVersion->getVersion()); - $this->assertEquals('Changed', $lastVersion->getTitle()); + $this->assertEquals('Teschd', $lastVersion->getTitle()); //age has not changed anything, so check as above $this->assertEquals(1, \ChangeloggerBehaviorMultipleAgeLogQuery::create()->count());