소스 검색

[translatable] refactor translate function for ORM repository, references #224

gedi 13 년 전
부모
커밋
e147788ca8

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

@@ -47,32 +47,32 @@ 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 ($this->_em->getUnitOfWork()->isInIdentityMap($entity)) {
-            if ($locale === $listener->getDefaultLocale()) {
-                $meta->getReflectionProperty($field)->setValue($entity, $value);
-                $this->_em->persist($entity);
-            } else {
-                $ea = new TranslatableAdapterORM();
-                $foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
-                $objectClass = $meta->name;
-                $class = $listener->getTranslationClass($ea, $meta->name);
-                $transMeta = $this->_em->getClassMetadata($class);
-                $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
-                if (!$trans) {
-                    $trans = new $class();
-                    $transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
-                    $transMeta->getReflectionProperty('objectClass')->setValue($trans, $objectClass);
-                    $transMeta->getReflectionProperty('field')->setValue($trans, $field);
-                    $transMeta->getReflectionProperty('locale')->setValue($trans, $locale);
-                }
-                $type = Type::getType($meta->getTypeOfField($field));
-                $transformed = $type->convertToDatabaseValue($value, $this->_em->getConnection()->getDatabasePlatform());
-                $transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
+        if ($locale === $listener->getDefaultLocale()) {
+            $meta->getReflectionProperty($field)->setValue($entity, $value);
+            $this->_em->persist($entity);
+        } else {
+            $ea = new TranslatableAdapterORM();
+            $foreignKey = $meta->getReflectionProperty($meta->getSingleIdentifierFieldName())->getValue($entity);
+            $objectClass = $meta->name;
+            $class = $listener->getTranslationClass($ea, $meta->name);
+            $transMeta = $this->_em->getClassMetadata($class);
+            $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
+            if (!$trans) {
+                $trans = new $class();
+                $transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
+                $transMeta->getReflectionProperty('objectClass')->setValue($trans, $objectClass);
+                $transMeta->getReflectionProperty('field')->setValue($trans, $field);
+                $transMeta->getReflectionProperty('locale')->setValue($trans, $locale);
+            }
+            $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);
             }
-        } else {
-            $oid = spl_object_hash($entity);
-            $listener->addTranslation($oid, $field, $locale, $value);
         }
         return $this;
     }

+ 5 - 0
lib/Gedmo/Translatable/TranslationListener.php

@@ -132,6 +132,11 @@ class TranslationListener extends MappedEventSubscriber
         return $this;
     }
 
+    public function addPendingTranslationInsert($oid, $translation)
+    {
+        $this->pendingTranslationInserts[$oid][] = $translation;
+    }
+
     /**
      * Add additional translation for $oid object
      *

+ 4 - 4
tests/Gedmo/Translatable/TranslatableEntityCollectionTest.php

@@ -40,13 +40,13 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
             'user' => 'root',
             'password' => 'nimda'
         );
-        $this->getMockCustomEntityManager($conn, $evm);
-        //$this->getMockSqliteEntityManager($evm);
+        //$this->getMockCustomEntityManager($conn, $evm);
+        $this->getMockSqliteEntityManager($evm);
         $this->populate();
     }
 
     /**
-     * test
+     * @test
      */
     function shouldPersistMultipleTranslations()
     {
@@ -70,7 +70,7 @@ class TranslatableEntityCollectionTest extends BaseTestCaseORM
     }
 
     /**
-     * test
+     * @test
      */
     function shouldUpdateTranslation()
     {