Forráskód Böngészése

[translatable] fix bug with repository translate method, closes #321

gedi 13 éve
szülő
commit
af3e4303fa

+ 10 - 3
lib/Gedmo/Translatable/Document/Repository/TranslationRepository.php

@@ -61,14 +61,21 @@ class TranslationRepository extends DocumentRepository
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
             throw new \Gedmo\Exception\InvalidArgumentException("Document: {$meta->name} does not translate field - {$field}");
             throw new \Gedmo\Exception\InvalidArgumentException("Document: {$meta->name} does not translate field - {$field}");
         }
         }
-        if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($document, $meta)))) {
+        $modRecordValue = (!$listener->getPersistDefaultLocaleTranslation() && $locale === $listener->getDefaultLocale())
+            || $listener->getTranslatableLocale($document, $meta) === $locale
+        ;
+        if ($modRecordValue) {
             $meta->getReflectionProperty($field)->setValue($document, $value);
             $meta->getReflectionProperty($field)->setValue($document, $value);
             $this->dm->persist($document);
             $this->dm->persist($document);
         } else {
         } else {
-            $ea = new TranslatableAdapterODM();
+            if (isset($config['translationClass'])) {
+                $class = $config['translationClass'];
+            } else {
+                $ea = new TranslatableAdapterODM();
+                $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
+            }
             $foreignKey = $meta->getReflectionProperty($meta->identifier)->getValue($document);
             $foreignKey = $meta->getReflectionProperty($meta->identifier)->getValue($document);
             $objectClass = $config['useObjectClass'];
             $objectClass = $config['useObjectClass'];
-            $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
             $transMeta = $this->dm->getClassMetadata($class);
             $transMeta = $this->dm->getClassMetadata($class);
             $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
             $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
             if (!$trans) {
             if (!$trans) {

+ 10 - 3
lib/Gedmo/Translatable/Entity/Repository/TranslationRepository.php

@@ -60,14 +60,21 @@ class TranslationRepository extends EntityRepository
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
         if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
             throw new \Gedmo\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate field - {$field}");
             throw new \Gedmo\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate field - {$field}");
         }
         }
-        if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($entity, $meta)))) {
+        $modRecordValue = (!$listener->getPersistDefaultLocaleTranslation() && $locale === $listener->getDefaultLocale())
+            || $listener->getTranslatableLocale($entity, $meta) === $locale
+        ;
+        if ($modRecordValue) {
             $meta->getReflectionProperty($field)->setValue($entity, $value);
             $meta->getReflectionProperty($field)->setValue($entity, $value);
             $this->_em->persist($entity);
             $this->_em->persist($entity);
         } else {
         } else {
-            $ea = new TranslatableAdapterORM();
+            if (isset($config['translationClass'])) {
+                $class = $config['translationClass'];
+            } else {
+                $ea = new TranslatableAdapterORM();
+                $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
+            }
             $foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
             $foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
             $objectClass = $config['useObjectClass'];
             $objectClass = $config['useObjectClass'];
-            $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
             $transMeta = $this->_em->getClassMetadata($class);
             $transMeta = $this->_em->getClassMetadata($class);
             $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
             $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
             if (!$trans) {
             if (!$trans) {

+ 11 - 0
lib/Gedmo/Translatable/TranslatableListener.php

@@ -145,6 +145,17 @@ class TranslatableListener extends MappedEventSubscriber
         return $this;
         return $this;
     }
     }
 
 
+    /**
+     * Check if should persist default locale
+     * translation or keep it in original record
+     *
+     * @return boolean
+     */
+    public function getPersistDefaultLocaleTranslation()
+    {
+        return (bool)$this->persistDefaultLocaleTranslation;
+    }
+
     /**
     /**
      * Add additional $translation for pending $oid object
      * Add additional $translation for pending $oid object
      * which is being inserted
      * which is being inserted

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

@@ -51,6 +51,7 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
     {
     {
         $this->translatableListener->setTranslatableLocale('de');
         $this->translatableListener->setTranslatableLocale('de');
         $this->translatableListener->setDefaultLocale('en');
         $this->translatableListener->setDefaultLocale('en');
+        $this->translatableListener->setPersistDefaultLocaleTranslation(true);
         $repo = $this->em->getRepository(self::TRANSLATION);
         $repo = $this->em->getRepository(self::TRANSLATION);
         $entity = new Article;
         $entity = new Article;
         $entity->setTitle('he'); // is translated to de
         $entity->setTitle('he'); // is translated to de
@@ -66,10 +67,11 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
         $this->em->flush();
         $this->em->flush();
         $this->em->clear();
         $this->em->clear();
         $trans = $repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
         $trans = $repo->findTranslations($this->em->find(self::ARTICLE, $entity->getId()));
-        $this->assertEquals(3, count($trans)); // EN is default, and left in original record
+        $this->assertCount(4, $trans);
         $this->assertSame('my article de', $trans['de']['title']); // overrides "he" which would be used if translate for de not called
         $this->assertSame('my article de', $trans['de']['title']); // overrides "he" which would be used if translate for de not called
         $this->assertSame('my article es', $trans['es']['title']);
         $this->assertSame('my article es', $trans['es']['title']);
         $this->assertSame('my article fr', $trans['fr']['title']);
         $this->assertSame('my article fr', $trans['fr']['title']);
+        $this->assertSame('my article en', $trans['en']['title']);
     }
     }
 
 
     /**
     /**