瀏覽代碼

translation repository update

gediminasm 14 年之前
父節點
當前提交
082118cd18

+ 34 - 10
lib/DoctrineExtensions/Translatable/Repository/TranslationRepository.php

@@ -34,16 +34,9 @@ class TranslationRepository extends EntityRepository
                 return $result;
             }
             $entityClass = get_class($entity);
-            // no need cache, metadata is loaded only once in MetadataFactoryClass
-            //$translationMetadata = $em->getClassMetadata(self::TRANSLATION_ENTITY_CLASS);
-            $entityClassMetadata = $this->_em->getClassMetadata($entityClass);
-            // check for the availability of the primary key
-            $entityId = $entityClassMetadata->getIdentifierValues($entity);
-            if (count($entityId) == 1 && current($entityId)) {
-                $entityId = current($entityId);
-            } else {
-                throw Exception::singleIdentifierRequired($entityClass);
-            }
+            $meta = $this->_em->getClassMetadata($entityClass);
+            $identifier = $meta->getSingleIdentifierFieldName();
+            $entityId = $meta->getReflectionProperty($identifier)->getValue($entity);
             
             $qb = $this->_em->createQueryBuilder();
             $qb->select('trans.content, trans.field, trans.locale')
@@ -65,6 +58,37 @@ class TranslationRepository extends EntityRepository
         return $result;
     }
     
+    /**
+     * Find the entity $class by the translated field.
+     * Result is the first occurence of translated field
+     * 
+     * @param string $field
+     * @param string $value
+     * @param string $class
+     * @return object - instance of $class or null if not found
+     */
+    public function findEntityByTranslatedField($field, $value, $class)
+    {
+    	$entity = null;
+    	$meta = $this->_em->getClassMetadata($class);
+    	if ($meta->hasField($field)) {
+    		$dql = "SELECT trans.foreignKey FROM {$this->_entityName} trans";
+    		$dql .= ' WHERE trans.entity = :class';
+    		$dql .= ' AND trans.field = :field';
+    		$dql .= ' AND trans.content = :value';
+    		$q = $this->_em->createQuery($dql);
+        	$q->setParameters(compact('class', 'field', 'value'));
+    		$q->setMaxResults(1);
+    		$result = $q->getArrayResult();
+    		$id = count($result) ? $result[0]['foreignKey'] : null;
+    			
+    		if ($id) {
+    			$entity = $this->_em->find($class, $id);
+    		}
+    	}
+    	return $entity;
+    }
+    
     /**
      * Loads all translations with all translatable
      * fields by a given entity primary key

+ 18 - 0
tests/DoctrineExtensions/Translatable/TranslatableIdentifierTest.php

@@ -86,6 +86,24 @@ class TranslatableIdentifierTest extends \PHPUnit_Framework_TestCase
         
         $repo = $this->em->getRepository(self::TRANSLATION_CLASS);
         
+        // test the entity load by translated title
+        $object = $repo->findEntityByTranslatedField(
+        	'title',
+        	'title in en',
+        	self::TEST_ENTITY_CLASS
+        );
+        
+        $this->assertTrue($object instanceof Translatable);
+        $this->assertEquals($this->testObjectId, $object->getUid());
+        
+        $object = $repo->findEntityByTranslatedField(
+        	'title',
+        	'title in de',
+        	self::TEST_ENTITY_CLASS
+        );
+        
+        $this->assertEquals($this->testObjectId, $object->getUid());
+        
         $translations = $repo->findTranslations($object);
         $this->assertEquals(count($translations), 2);
         $this->assertArrayHasKey('de_de', $translations);