Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 52ce0b0

Browse files
committed
[#145] Make sure locale doesn't change after flush is called
1 parent 0347e26 commit 52ce0b0

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

Adapter/PhpcrOdmAdapter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ public function getLocales($contentDocument)
7171
public function translateObject($contentDocument, $locale)
7272
{
7373
$meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument));
74-
$contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale);
7574

76-
return $contentDocument;
75+
return $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale);
7776
}
7877

7978
/**

Doctrine/Phpcr/AutoRouteListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function onFlush(ManagerEventArgs $args)
6363
$autoRoute = null;
6464
foreach ($updates as $document) {
6565
if ($this->isAutoRouteable($document)) {
66+
$locale = $uow->getCurrentLocale($document);
6667

6768
$uriContextCollection = new UriContextCollection($document);
6869
$arm->buildUriContextCollection($uriContextCollection);
@@ -73,6 +74,11 @@ public function onFlush(ManagerEventArgs $args)
7374
$dm->persist($autoRoute);
7475
$uow->computeChangeSets();
7576
}
77+
78+
// reset locale to the original locale
79+
if (null !== $locale) {
80+
$dm->findTranslation(get_class($document), $uow->getDocumentId($document), $locale);
81+
}
7682
}
7783
}
7884

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212

13-
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\Subscriber;
13+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\EventListener;
1414

1515
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\BaseTestCase;
1616
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog;
@@ -229,7 +229,6 @@ public function testMultilangArticle($data, $expectedPaths)
229229
$this->getDm()->flush();
230230
$this->getDm()->clear();
231231

232-
$articleTitles = array_values($data);
233232
$locales = array_keys($data);
234233

235234
foreach ($expectedPaths as $i => $expectedPath) {
@@ -271,6 +270,27 @@ public function provideUpdateMultilangArticle()
271270
);
272271
}
273272

273+
public function testMultilangArticleRemainsSameLocale()
274+
{
275+
$article = new Article;
276+
$article->path = '/test/article-1';
277+
$this->getDm()->persist($article);
278+
279+
$article->title = 'Hello everybody!';
280+
$this->getDm()->bindTranslation($article, 'en');
281+
282+
$article->title = 'Bonjour le monde!';
283+
$this->getDm()->bindTranslation($article, 'fr');
284+
285+
// let current article be something else than the last bound locale
286+
$this->getDm()->findTranslation(get_class($article), $this->getDm()->getUnitOfWork()->getDocumentId($article), 'en');
287+
288+
$this->getDm()->flush();
289+
$this->getDm()->clear();
290+
291+
$this->assertEquals('Hello everybody!', $article->title);
292+
}
293+
274294
/**
275295
* @dataProvider provideUpdateMultilangArticle
276296
*/

Tests/Resources/Document/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Post
4040
public $blog;
4141

4242
/**
43-
* @PHPCR\NodeName()
43+
* @PHPCR\Nodename()
4444
*/
4545
public $name;
4646

0 commit comments

Comments
 (0)