소스 검색

[translatable] fix issue of multiple translation persistense in case if currently used locale, closes #234

gedi 13 년 전
부모
커밋
3b2e2dc08f

+ 1 - 1
lib/Gedmo/Translatable/Document/Repository/TranslationRepository.php

@@ -47,7 +47,7 @@ class TranslationRepository extends DocumentRepository
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
             throw new \Gedmo\Exception\InvalidArgumentException("Document: {$meta->name} does not translate field - {$field}");
         }
-        if ($locale === $listener->getDefaultLocale()) {
+        if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($document, $meta)))) {
             $meta->getReflectionProperty($field)->setValue($document, $value);
             $this->dm->persist($document);
         } else {

+ 1 - 1
lib/Gedmo/Translatable/Entity/Repository/TranslationRepository.php

@@ -47,7 +47,7 @@ class TranslationRepository extends EntityRepository
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
             throw new \Gedmo\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate field - {$field}");
         }
-        if ($locale === $listener->getDefaultLocale()) {
+        if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($entity, $meta)))) {
             $meta->getReflectionProperty($field)->setValue($entity, $value);
             $this->_em->persist($entity);
         } else {

+ 25 - 1
tests/Gedmo/Translatable/TranslatableEntityCollectionTest.php

@@ -42,7 +42,28 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
         );
         //$this->getMockCustomEntityManager($conn, $evm);
         $this->getMockSqliteEntityManager($evm);
-        $this->populate();
+    }
+
+    /**
+     * @test
+     */
+    function shouldEnsureSolvedIssue234()
+    {
+        $this->translatableListener->setTranslatableLocale('de');
+        $this->translatableListener->setDefaultLocale('en');
+        $repo = $this->em->getRepository(self::TRANSLATION);
+        $entity = new Article;
+        $entity->setTitle('he'); // is translated to de
+
+        $repo
+            ->translate($entity, 'title', 'en', 'my article en')
+            ->translate($entity, 'title', 'es', 'my article es')
+            ->translate($entity, 'title', 'fr', 'my article fr')
+            ->translate($entity, 'title', 'de', 'my article de')
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
     }
 
     /**
@@ -50,6 +71,7 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
      */
     function shouldPersistMultipleTranslations()
     {
+        $this->populate();
         $repo = $this->em->getRepository(self::TRANSLATION);
         $sport = $this->em->getRepository(self::ARTICLE)->find(1);
         $translations = $repo->findTranslations($sport);
@@ -74,6 +96,7 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
      */
     function shouldUpdateTranslation()
     {
+        $this->populate();
         $repo = $this->em->getRepository(self::TRANSLATION);
         $sport = $this->em->getRepository(self::ARTICLE)->find(1);
         $repo
@@ -97,6 +120,7 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
      */
     function shouldUpdateMultipleTranslations()
     {
+        $this->populate();
         $repo = $this->em->getRepository(self::TRANSLATION);
         $sport = $this->em->getRepository(self::ARTICLE)->find(1);
         $repo