Skip to content

Commit d3c7808

Browse files
committed
Merge pull request #615 from doctrine/pre_bind_translation
Added preBind translation
2 parents 69cfbdc + b8e3519 commit d3c7808

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

lib/Doctrine/ODM/PHPCR/Event.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ final class Event
4040

4141
const postLoadTranslation = 'postLoadTranslation';
4242
const preCreateTranslation = 'preCreateTranslation';
43+
const preUpdateTranslation = 'preUpdateTranslation';
4344
const preRemoveTranslation = 'preRemoveTranslation';
4445
const postRemoveTranslation = 'postRemoveTranslation';
4546

@@ -55,6 +56,7 @@ final class Event
5556
self::postLoad => self::postLoad,
5657
self::postLoadTranslation => self::postLoadTranslation,
5758
self::preCreateTranslation => self::preCreateTranslation,
59+
self::preUpdateTranslation => self::preUpdateTranslation,
5860
self::preRemoveTranslation => self::preRemoveTranslation,
5961
self::postRemoveTranslation => self::postRemoveTranslation,
6062
);

lib/Doctrine/ODM/PHPCR/UnitOfWork.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,14 @@ private function doBindTranslation($document, $locale, ClassMetadata $class)
753753
new LifecycleEventArgs($document, $this->dm),
754754
$invoke
755755
);
756+
} elseif ($invoke = $this->eventListenersInvoker->getSubscribedSystems($class, Event::preUpdateTranslation)) {
757+
$this->eventListenersInvoker->invoke(
758+
$class,
759+
Event::preUpdateTranslation,
760+
$document,
761+
new LifecycleEventArgs($document, $this->dm),
762+
$invoke
763+
);
756764
}
757765

758766
$this->setLocale($document, $class, $locale);

tests/Doctrine/Tests/ODM/PHPCR/Functional/EventComputingTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function testComputingBetweenEventsWithTranslation()
116116
->addEventListener(
117117
array(
118118
Event::preCreateTranslation,
119+
Event::preUpdateTranslation,
119120
Event::postLoadTranslation,
120121
Event::preRemoveTranslation,
121122
Event::postRemoveTranslation,
@@ -142,8 +143,13 @@ public function testComputingBetweenEventsWithTranslation()
142143
// name had been changed pre binding translation
143144
$this->assertEquals('preCreateTranslation', $user->name);
144145

146+
$this->dm->bindTranslation($user, 'en');
147+
// name has been changed when translation was updated
148+
$this->assertEquals('preUpdateTranslation', $user->name);
149+
145150
$this->dm->name = 'neuer Name';
146151
$this->dm->bindTranslation($user, 'de');
152+
147153
$this->dm->flush();
148154
$this->dm->clear();
149155

@@ -215,6 +221,12 @@ public function preCreateTranslation(LifecycleEventArgs $e)
215221
$document->name = 'preCreateTranslation';
216222
}
217223

224+
public function preUpdateTranslation(LifecycleEventArgs $e)
225+
{
226+
$document = $e->getObject();
227+
$document->name = 'preUpdateTranslation';
228+
}
229+
218230
public function postLoadTranslation(LifecycleEventArgs $e)
219231
{
220232
$document = $e->getObject();

0 commit comments

Comments
 (0)