소스 검색

Avoid persisting unwanted translation entities for default locale

Previously, when the translatable locale was different from default
locale, and translation was for the default locale, a translation
entity was persisted regardless of persistDefaultLocaleTranslation.
Christian Aistleitner 12 년 전
부모
커밋
a36d50ecf5

+ 9 - 5
lib/Gedmo/Translatable/Entity/Repository/TranslationRepository.php

@@ -60,6 +60,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}");
         }
+        $needsPersist = TRUE;
         if ($locale === $listener->getTranslatableLocale($entity, $meta)) {
             $meta->getReflectionProperty($field)->setValue($entity, $value);
             $this->_em->persist($entity);
@@ -83,16 +84,19 @@ class TranslationRepository extends EntityRepository
                 if ($listener->getDefaultLocale() != $listener->getTranslatableLocale($entity, $meta) &&
                     $locale === $listener->getDefaultLocale()) {
                     $listener->setTranslationInDefaultLocale(spl_object_hash($entity), $trans);
+                    $needsPersist = $listener->getPersistDefaultLocaleTranslation();
                 }
             }
             $type = Type::getType($meta->getTypeOfField($field));
             $transformed = $type->convertToDatabaseValue($value, $this->_em->getConnection()->getDatabasePlatform());
             $transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
-            if ($this->_em->getUnitOfWork()->isInIdentityMap($entity)) {
-                $this->_em->persist($trans);
-            } else {
-                $oid = spl_object_hash($entity);
-                $listener->addPendingTranslationInsert($oid, $trans);
+            if ($needsPersist) {
+                if ($this->_em->getUnitOfWork()->isInIdentityMap($entity)) {
+                    $this->_em->persist($trans);
+                } else {
+                    $oid = spl_object_hash($entity);
+                    $listener->addPendingTranslationInsert($oid, $trans);
+                }
             }
         }
         return $this;

+ 189 - 0
tests/Gedmo/Translatable/TranslatableEntityDefaultTranslationTest.php

@@ -100,6 +100,195 @@ class TranslatableEntityDefaultTranslationTest extends BaseTestCaseORM
 
 
 
+    // --- Tests for default translation making it into the entity's
+    //     database row --------------------------------------------------------
+
+
+
+    function testOnlyDefaultTranslationWithoutPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( false );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(0, $trans);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+    function testOnlyDefaultTranslationWithPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( true );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(1, $trans);
+        $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+    /**
+     * As this test does not provide a default translation, we assert
+     * that a translated value is picked as default value
+     */
+    function testOnlyEntityTranslationWithoutPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( false );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(1, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title translatedLocale', $articles[0]['title']);
+    }
+
+    /**
+     * As this test does not provide a default translation, we assert
+     * that a translated value is picked as default value
+     */
+    function testOnlyEntityTranslationWithPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( true );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(1, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title translatedLocale', $articles[0]['title']);
+    }
+
+    function testDefaultAndEntityTranslationWithoutPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( false );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(1, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+    function testDefaultAndEntityTranslationWithoutPersistingDefaultResorted()
+    {
+
+        $this->translatableListener->setPersistDefaultLocaleTranslation( false );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(1, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+    function testDefaultAndEntityTranslationWithPersistingDefault()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( true );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(2, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+        $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+    function testDefaultAndEntityTranslationWithPersistingDefaultResorted()
+    {
+        $this->translatableListener->setPersistDefaultLocaleTranslation( true );
+        $entity = new Article;
+        $this->repo
+            ->translate($entity, 'title', 'translatedLocale', 'title translatedLocale')
+            ->translate($entity, 'title', 'defaultLocale'   , 'title defaultLocale'   )
+        ;
+
+        $this->em->persist($entity);
+        $this->em->flush();
+        $this->em->clear();
+
+        $trans = $this->repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
+        $this->assertCount(2, $trans);
+        $this->assertSame('title translatedLocale', $trans['translatedLocale']['title']);
+        $this->assertSame('title defaultLocale', $trans['defaultLocale']['title']);
+
+        $articles = $this->em->createQuery('SELECT a FROM ' . self::ARTICLE . ' a')->getArrayResult();
+        $this->assertCount(1, $articles);
+        $this->assertEquals('title defaultLocale', $articles[0]['title']);
+    }
+
+
+
     // --- Fixture related methods ---------------------------------------------