Просмотр исходного кода

[translatable] adapt personal translation in mongo ODM

gedi 13 лет назад
Родитель
Сommit
de359a755e

+ 27 - 34
lib/Gedmo/Translatable/Mapping/Event/Adapter/ODM.php

@@ -54,52 +54,46 @@ final class ODM extends BaseAdapterODM implements TranslatableAdapter
 
 
         if ($this->usesPersonalTranslation($translationClass)) {
         if ($this->usesPersonalTranslation($translationClass)) {
             // first try to load it using collection
             // first try to load it using collection
-            die(print_r($wrapped->getMetadata()));
-            $found = false;
-            foreach ($wrapped->getMetadata()->associationMappings as $assoc) {
-                $isRightCollection = $assoc['targetEntity'] === $translationClass
-                && $assoc['mappedBy'] === 'object'
-                && $assoc['type'] === ClassMetadataInfo::ONE_TO_MANY
+            foreach ($wrapped->getMetadata()->fieldMappings as $mapping) {
+                $isRightCollection = isset($mapping['association'])
+                    && $mapping['association'] === ClassMetadataInfo::REFERENCE_MANY
+                    && $mapping['targetDocument'] === $translationClass
+                    && $mapping['mappedBy'] === 'object'
                 ;
                 ;
                 if ($isRightCollection) {
                 if ($isRightCollection) {
-                    $collection = $wrapped->getPropertyValue($assoc['fieldName']);
+                    $collection = $wrapped->getPropertyValue($mapping['fieldName']);
                     foreach ($collection as $trans) {
                     foreach ($collection as $trans) {
                         if ($trans->getLocale() === $locale) {
                         if ($trans->getLocale() === $locale) {
                             $result[] = array(
                             $result[] = array(
-                                        'field' => $trans->getField(),
-                                        'content' => $trans->getContent()
+                                'field' => $trans->getField(),
+                                'content' => $trans->getContent()
                             );
                             );
                         }
                         }
                     }
                     }
-                    $found = true;
-                    break;
+                    return $result;
                 }
                 }
             }
             }
-            // if collection is not set, fetch it through relation
-            if (!$found) {
-                $dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
-                $dql .= ' WHERE t.locale = :locale';
-                $dql .= ' AND t.object = :object';
-
-                $q = $em->createQuery($dql);
-                $q->setParameters(compact('object', 'locale'));
-                $result = $q->getArrayResult();
-            }
+            $q = $dm
+                ->createQueryBuilder($translationClass)
+                ->field('object.$id')->equals($wrapped->getIdentifier())
+                ->field('locale')->equals($locale)
+                ->getQuery()
+            ;
         } else {
         } else {
             // load translated content for all translatable fields
             // load translated content for all translatable fields
-            $identifier = $wrapped->getIdentifier();
             // construct query
             // construct query
-            $qb = $dm->createQueryBuilder($translationClass);
-            $q = $qb->field('foreignKey')->equals($identifier)
+            $q = $dm
+                ->createQueryBuilder($translationClass)
+                ->field('foreignKey')->equals($wrapped->getIdentifier())
                 ->field('locale')->equals($locale)
                 ->field('locale')->equals($locale)
                 ->field('objectClass')->equals($wrapped->getMetadata()->name)
                 ->field('objectClass')->equals($wrapped->getMetadata()->name)
-                ->getQuery();
-
-            $q->setHydrate(false);
-            $result = $q->execute();
-            if ($result instanceof Cursor) {
-                $result = $result->toArray();
-            }
+                ->getQuery()
+            ;
+        }
+        $q->setHydrate(false);
+        $result = $q->execute();
+        if ($result instanceof Cursor) {
+            $result = $result->toArray();
         }
         }
         return $result;
         return $result;
     }
     }
@@ -117,13 +111,12 @@ final class ODM extends BaseAdapterODM implements TranslatableAdapter
             ->limit(1)
             ->limit(1)
         ;
         ;
         if ($this->usesPersonalTranslation($translationClass)) {
         if ($this->usesPersonalTranslation($translationClass)) {
-            $qb->field('object')->equals($wrapped->getObject());
+            $qb->field('object.$id')->equals($wrapped->getIdentifier());
         } else {
         } else {
             $qb->field('foreignKey')->equals($wrapped->getIdentifier());
             $qb->field('foreignKey')->equals($wrapped->getIdentifier());
             $qb->field('objectClass')->equals($wrapped->getMetadata()->name);
             $qb->field('objectClass')->equals($wrapped->getMetadata()->name);
         }
         }
         $q = $qb->getQuery();
         $q = $qb->getQuery();
-        $q->setHydrate(false);
         $result = $q->execute();
         $result = $q->execute();
         if ($result instanceof Cursor) {
         if ($result instanceof Cursor) {
             $result = current($result->toArray());
             $result = current($result->toArray());
@@ -142,7 +135,7 @@ final class ODM extends BaseAdapterODM implements TranslatableAdapter
             ->remove()
             ->remove()
         ;
         ;
         if ($this->usesPersonalTranslation($transClass)) {
         if ($this->usesPersonalTranslation($transClass)) {
-            $qb->field('object')->equals($wrapped->getObject());
+            $qb->field('object.$id')->equals($wrapped->getIdentifier());
         } else {
         } else {
             $qb->field('foreignKey')->equals($wrapped->getIdentifier());
             $qb->field('foreignKey')->equals($wrapped->getIdentifier());
             $qb->field('objectClass')->equals($wrapped->getMetadata()->name);
             $qb->field('objectClass')->equals($wrapped->getMetadata()->name);

+ 1 - 2
tests/Gedmo/Translatable/PersonalTranslationDocumentTest.php

@@ -45,8 +45,7 @@ class PersonalTranslationDocumentTest extends BaseTestCaseMongoODM
         $article = $this->dm->getRepository(self::ARTICLE)->find($this->id);
         $article = $this->dm->getRepository(self::ARTICLE)->find($this->id);
         $translations = $article->getTranslations();
         $translations = $article->getTranslations();
 
 
-        // @TODO: strange issue, persisted object is not inserted
-        //$this->assertEquals(2, count($translations));
+        $this->assertEquals(2, count($translations));
     }
     }
 
 
     /**
     /**