Explorar o código

Added support of nofallback annotation

Dmitry Pikhno %!s(int64=13) %!d(string=hai) anos
pai
achega
7a79c7c6c6

+ 1 - 0
lib/Gedmo/Mapping/Annotation/All.php

@@ -19,6 +19,7 @@ include __DIR__.'/SortableGroup.php';
 include __DIR__.'/SortablePosition.php';
 include __DIR__.'/Timestampable.php';
 include __DIR__.'/Translatable.php';
+include __DIR__.'/NoFallback.php';
 include __DIR__.'/TranslationEntity.php';
 include __DIR__.'/Tree.php';
 include __DIR__.'/TreeClosure.php';

+ 18 - 0
lib/Gedmo/Mapping/Annotation/NoFallback.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Gedmo\Mapping\Annotation;
+
+use Doctrine\Common\Annotations\Annotation;
+
+/**
+ * NoFallback annotation for Timestampable behavioral extension
+ *
+ * @Annotation
+ * @Target("PROPERTY")
+ *
+ * @author Dmitry Pikhno
+ */
+final class NoFallback extends Annotation
+{
+
+}

+ 9 - 0
lib/Gedmo/Translatable/Mapping/Driver/Annotation.php

@@ -29,6 +29,11 @@ class Annotation implements AnnotationDriverInterface
      */
     const TRANSLATABLE = 'Gedmo\\Mapping\\Annotation\\Translatable';
 
+	/**
+	 * Annotation to identify field that does not require fallback
+	 */
+	const NOFALLBACK = 'Gedmo\\Mapping\\Annotation\\NoFallback';
+
     /**
      * Annotation to identify field which can store used locale or language
      * alias is LANGUAGE
@@ -81,6 +86,7 @@ class Annotation implements AnnotationDriverInterface
             $config['translationClass'] = $annot->class;
         }
 
+		$config['nofallback'] = array();
         // property annotations
         foreach ($class->getProperties() as $property) {
             if ($meta->isMappedSuperclass && !$property->isPrivate() ||
@@ -97,6 +103,9 @@ class Annotation implements AnnotationDriverInterface
                 }
                 // fields cannot be overrided and throws mapping exception
                 $config['fields'][] = $field;
+				if ($this->reader->getPropertyAnnotation($property, self::NOFALLBACK)) {
+					$config['nofallback'][] = $field;
+				}
             }
             // locale property
             if ($locale = $this->reader->getPropertyAnnotation($property, self::LOCALE)) {

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

@@ -63,6 +63,9 @@ class Xml extends BaseXml
                 $field = $this->_getAttribute($mappingDoctrine, 'name');
                 if (isset($mapping->translatable)) {
                     $config['fields'][] = $field;
+					if (isset($mapping->nofallback)) {
+						$config['nofallback'][] = $field;
+					}
                 }
             }
         }

+ 3 - 0
lib/Gedmo/Translatable/Mapping/Driver/Yaml.php

@@ -54,6 +54,9 @@ class Yaml extends File implements Driver
                     if (in_array('translatable', $fieldMapping['gedmo'])) {
                         // fields cannot be overrided and throws mapping exception
                         $config['fields'][] = $field;
+						if (in_array('nofallback', $fieldMapping['gedmo'])) {
+							$config['nofallback'][] = $field;
+						}
                     }
                 }
             }

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

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

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

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

+ 1 - 0
schemas/orm/doctrine-extensions-mapping-2-1.xsd

@@ -68,6 +68,7 @@ people to push their own additional attributes/elements into the same field elem
   <xs:element name="sluggable" type="gedmo:sluggable"/>
   <xs:element name="slug" type="gedmo:slug"/>
   <xs:element name="translatable" type="gedmo:emptyType"/>
+  <xs:element name="nofallback" type="gedmo:emptyType"/>
   <xs:element name="timestampable" type="gedmo:timestampable"/>
   <xs:element name="versioned" type="gedmo:emptyType"/>
   <xs:element name="tree-left" type="gedmo:emptyType"/>

+ 1 - 0
schemas/orm/doctrine-extensions-mapping-2-2.xsd

@@ -30,6 +30,7 @@ people to push their own additional attributes/elements into the same field elem
   <!-- field -->
   <xs:element name="slug" type="gedmo:slug"/>
   <xs:element name="translatable" type="gedmo:emptyType"/>
+  <xs:element name="nofallback" type="gedmo:emptyType"/>
   <xs:element name="timestampable" type="gedmo:timestampable"/>
   <xs:element name="versioned" type="gedmo:emptyType"/>
   <xs:element name="tree-left" type="gedmo:emptyType"/>

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

@@ -28,6 +28,7 @@ class Article implements Translatable
 
     /**
      * @Gedmo\Translatable
+	 * @Gedmo\NoFallback
      * @ORM\Column(name="views", type="integer", nullable=true)
      */
     private $views;

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

@@ -247,6 +247,27 @@ class TranslatableTest extends BaseTestCaseORM
         $this->assertCount(1, $translations);
     }
 
+	function shouldRespectNoFallbackAnnotation()
+	{
+		$article = new Article;
+		$article->setTitle('Euro2012');
+		$article->setViews(10);
+
+		$this->em->persist($article);
+		$this->em->flush();
+
+		$this->translatableListener->setTranslatableLocale('ua_UA');
+		$this->em->persist($article);
+		$this->em->flush();
+
+		$this->em->clear();
+		$this->translatableListener->setTranslationFallback(true);
+		$article = $this->em->find(self::ARTICLE, $article->getId());
+
+		$this->assertEquals('Euro2012', $article->getTitle());
+		$this->assertEmpty($article->getViews());
+	}
+
     protected function getUsedEntityFixtures()
     {
         return array(