فهرست منبع

added fallback=true to handle force fallback;
added test for walker

Dmitry Pikhno 13 سال پیش
والد
کامیت
5ac04c494f

+ 1 - 1
lib/Gedmo/Mapping/Annotation/Translatable.php

@@ -19,6 +19,6 @@ use Doctrine\Common\Annotations\Annotation;
 final class Translatable extends Annotation
 {
     /** @var boolean */
-    public $fallback = true;
+    public $fallback;
 }
 

+ 2 - 2
lib/Gedmo/Translatable/Mapping/Driver/Annotation.php

@@ -97,8 +97,8 @@ class Annotation implements AnnotationDriverInterface
                 }
                 // fields cannot be overrided and throws mapping exception
                 $config['fields'][] = $field;
-                if (false === $translatable->fallback) {
-                    $config['nofallback'][] = $field;
+                if(isset($translatable->fallback)) {
+                    $config['fallback'][$field] = (bool)$translatable->fallback;
                 }
             }
             // locale property

+ 3 - 5
lib/Gedmo/Translatable/Mapping/Driver/Xml.php

@@ -63,12 +63,10 @@ class Xml extends BaseXml
                 $field = $this->_getAttribute($mappingDoctrine, 'name');
                 if (isset($mapping->translatable)) {
                     $config['fields'][] = $field;
-                    /**
-                     * @var \SimpleXmlElement $data
-                     */
+                    /** @var \SimpleXmlElement $data */
                     $data = $mapping->translatable;
-                    if($this->_isAttributeSet($data, 'fallback') && false === $this->_getAttribute($data, 'fallback')) {
-                        $config['nofallback'][] = $field;
+                    if ($this->_isAttributeSet($data, 'fallback')) {
+                        $config['fallback'][$field] = (bool)$this->_getAttribute($data, 'fallback');
                     }
                 }
             }

+ 2 - 2
lib/Gedmo/Translatable/Mapping/Driver/Yaml.php

@@ -55,8 +55,8 @@ class Yaml extends File implements Driver
                         // fields cannot be overrided and throws mapping exception
                         $config['fields'][] = $field;
                         $mappingProperty = $fieldMapping['gedmo']['translatable'];
-                        if (isset($mappingProperty['fallback']) && false === $mappingProperty['fallback']) {
-                            $config['nofallback'][] = $field;
+                        if (isset($mappingProperty['fallback'])) {
+                            $config['fallback'][$field] = (bool)$mappingProperty['fallback'];
                         }
                     }
                 }

+ 3 - 1
lib/Gedmo/Translatable/Query/TreeWalker/TranslationWalker.php

@@ -305,7 +305,9 @@ class TranslationWalker extends SqlWalker
                 }
 
                 // Fallback to original if was asked for
-                if ($this->needsFallback() && (!isset($config['nofallback']) || !in_array($field, $config['nofallback']))) {
+                if (($this->needsFallback() && (!isset($config['fallback'][$field]) || $config['fallback'][$field]))
+                    ||  (!$this->needsFallback() && isset($config['fallback'][$field]) && $config['fallback'][$field])
+                ) {
                     $substituteField = 'COALESCE('.$substituteField.', '.$originalField.')';
                 }
 

+ 4 - 1
lib/Gedmo/Translatable/TranslatableListener.php

@@ -412,7 +412,10 @@ class TranslatableListener extends MappedEventSubscriber
                     }
                 }
                 // update translation
-                if ($translated || !$this->translationFallback  || (isset($config['nofallback']) && in_array($field, $config['nofallback']))) {
+                if ($translated
+                    || (!$this->translationFallback && (!isset($config['fallback'][$field]) || !$config['fallback'][$field]))
+                    || ($this->translationFallback && isset($config['fallback'][$field]) && !$config['fallback'][$field])
+                ) {
                     $ea->setTranslationValue($object, $field, $translated);
                     // ensure clean changeset
                     $ea->setOriginalObjectProperty(

+ 16 - 0
tests/Gedmo/Translatable/Fixture/Article.php

@@ -32,6 +32,12 @@ class Article implements Translatable
      */
     private $views;
 
+    /**
+     * @Gedmo\Translatable(fallback=true)
+     * @ORM\Column(name="author", type="string", nullable=true)
+     */
+    private $author;
+
     /**
      * Used locale to override Translation listener`s locale
      * @Gedmo\Locale
@@ -93,4 +99,14 @@ class Article implements Translatable
     {
         return $this->views;
     }
+
+    public function setAuthor($author)
+    {
+        $this->author = $author;
+    }
+
+    public function getAuthor()
+    {
+        return $this->author;
+    }
 }

+ 9 - 0
tests/Gedmo/Translatable/TranslatableTest.php

@@ -254,6 +254,7 @@ class TranslatableTest extends BaseTestCaseORM
     {
         $article = new Article;
         $article->setTitle('Euro2012');
+        $article->setAuthor('Shevchenko');
         $article->setViews(10);
 
         $this->em->persist($article);
@@ -265,6 +266,14 @@ class TranslatableTest extends BaseTestCaseORM
         $article = $this->em->find(self::ARTICLE, $article->getId());
 
         $this->assertEquals('Euro2012', $article->getTitle());
+        $this->assertEquals('Shevchenko', $article->getAuthor());
+        $this->assertEmpty($article->getViews());
+
+        $this->em->clear();
+        $this->translatableListener->setTranslationFallback(false);
+        $article = $this->em->find(self::ARTICLE, $article->getId());
+        $this->assertEmpty($article->getTitle());
+        $this->assertEquals('Shevchenko', $article->getAuthor());
         $this->assertEmpty($article->getViews());
     }
 

+ 39 - 4
tests/Gedmo/Translatable/TranslationQueryWalkerTest.php

@@ -187,6 +187,40 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
         $this->assertEquals('about food', $result[0]['content']);
     }
 
+    public function testSelectWithOptionalFallbackOnSimpleObjectHydration()
+    {
+        $this->em
+            ->getConfiguration()
+            ->expects($this->any())
+            ->method('getCustomHydrationMode')
+            ->with(TranslationWalker::HYDRATE_SIMPLE_OBJECT_TRANSLATION)
+            ->will($this->returnValue('Gedmo\\Translatable\\Hydrator\\ORM\\SimpleObjectHydrator'));
+
+        $dql = 'SELECT a FROM ' . self::ARTICLE . ' a';
+        $q = $this->em->createQuery($dql);
+        $q->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, self::TREE_WALKER_TRANSLATION);
+
+        $this->translatableListener->setTranslatableLocale('ru_ru');
+        $this->translatableListener->setTranslationFallback(false);
+
+        // simple object hydration
+        $this->startQueryLog();
+        $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
+        $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
+        $this->assertEquals('', $result[0]->getTitle());
+        $this->assertEquals('John Doe', $result[0]->getAuthor()); // optional fallback is true,  force fallback
+        $this->assertEquals(0, $result[0]->getViews());
+
+        $this->translatableListener->setTranslationFallback(true);
+        $this->queryAnalyzer->cleanUp();
+        $result = $q->getResult(Query::HYDRATE_SIMPLEOBJECT);
+        $this->assertEquals(1, $this->queryAnalyzer->getNumExecutedQueries());
+        //Default translation is en_us, so we expect the results in that locale
+        $this->assertEquals('Food', $result[0]->getTitle());
+        $this->assertEquals('John Doe', $result[0]->getAuthor());
+        $this->assertEquals(0, $result[0]->getViews()); // optional fallback is false,  thus no translation required
+    }
+
     /**
      * @test
      */
@@ -400,7 +434,7 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
         $result = $q->getArrayResult();
         $this->assertCount(1, $result);
         $food = $result[0];
-        $this->assertCount(5, $food);
+        $this->assertCount(6, $food);
         $this->assertEquals('Food', $food['title']);
         $this->assertEquals('about food', $food['content']);
         $comments = $food['comments'];
@@ -418,7 +452,7 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
         $result = $q->getArrayResult();
         $this->assertCount(1, $result);
         $food = $result[0];
-        $this->assertCount(5, $food);
+        $this->assertCount(6, $food);
         $this->assertEquals('Maistas', $food['title']);
         $this->assertEquals('apie maista', $food['content']);
         $comments = $food['comments'];
@@ -525,14 +559,14 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
         $result = $q->getArrayResult();
         $this->assertCount(1, $result);
         $food = $result[0];
-        $this->assertCount(4, $food);
+        $this->assertCount(5, $food);
         $this->assertEquals('Food', $food['title']);
         $this->assertEquals('about food', $food['content']);
         $this->translatableListener->setTranslatableLocale('lt_lt');
         $result = $q->getArrayResult();
         $this->assertCount(1, $result);
         $food = $result[0];
-        $this->assertCount(4, $food);
+        $this->assertCount(5, $food);
         $this->assertEquals('Maistas', $food['title']);
         $this->assertEquals('apie maista', $food['content']);
 
@@ -638,6 +672,7 @@ class TranslationQueryWalkerTest extends BaseTestCaseORM
         $food = new Article;
         $food->setTitle('Food');
         $food->setContent('about food');
+        $food->setAuthor('John Doe');
         $food->setViews(99);
 
         $goodFood = new Comment;