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

Make sure locale doesn't change after flush is called #159

Merged
merged 3 commits into from
Oct 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Adapter/PhpcrOdmAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public function getLocales($contentDocument)
public function translateObject($contentDocument, $locale)
{
$meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument));
$contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale);

return $contentDocument;
return $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions Doctrine/Phpcr/AutoRouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function onFlush(ManagerEventArgs $args)
$autoRoute = null;
foreach ($updates as $document) {
if ($this->isAutoRouteable($document)) {
$locale = $uow->getCurrentLocale($document);

$uriContextCollection = new UriContextCollection($document);
$arm->buildUriContextCollection($uriContextCollection);
Expand All @@ -73,6 +74,11 @@ public function onFlush(ManagerEventArgs $args)
$dm->persist($autoRoute);
$uow->computeChangeSets();
}

// reset locale to the original locale
if (null !== $locale) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could compare locale with $uow->getCurrentLocale to check if its needed to reset the translation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As lukas mentioned findTranslation was caching the items, I was a bit unsure what would be better, performance-wise.

$dm->findTranslation(get_class($document), $uow->getDocumentId($document), $locale);
}
}
}

Expand Down
26 changes: 24 additions & 2 deletions Tests/Functional/EventListener/AutoRouteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/


namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\Subscriber;
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Functional\EventListener;

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

$articleTitles = array_values($data);
$locales = array_keys($data);

foreach ($expectedPaths as $i => $expectedPath) {
Expand Down Expand Up @@ -271,6 +270,29 @@ public function provideUpdateMultilangArticle()
);
}

public function testMultilangArticleRemainsSameLocale()
{
$article = new Article;
$article->path = '/test/article-1';
$article->title = 'Good Day';
$this->getDm()->persist($article);
$this->getDm()->flush();

$article->title = 'Hello everybody!';
$this->getDm()->bindTranslation($article, 'en');

$article->title = 'Bonjour le monde!';
$this->getDm()->bindTranslation($article, 'fr');

// let current article be something else than the last bound locale
$this->getDm()->findTranslation(get_class($article), $this->getDm()->getUnitOfWork()->getDocumentId($article), 'en');

$this->getDm()->flush();
$this->getDm()->clear();

$this->assertEquals('Hello everybody!', $article->title);
}

/**
* @dataProvider provideUpdateMultilangArticle
*/
Expand Down
2 changes: 1 addition & 1 deletion Tests/Resources/Document/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Post
public $blog;

/**
* @PHPCR\NodeName()
* @PHPCR\Nodename()
*/
public $name;

Expand Down