Преглед на файлове

repository method for finding all translations

gediminasm преди 15 години
родител
ревизия
3649d793b9
променени са 2 файла, в които са добавени 83 реда и са изтрити 43 реда
  1. 33 0
      README.markdown
  2. 50 43
      lib/DoctrineExtensions/Translatable/Repository/TranslationRepository.php

+ 33 - 0
README.markdown

@@ -135,3 +135,36 @@ To save article with its translations:
     $em->persist($article);
     $em->flush();
     
+Update in other locale:
+
+    $article->setTitle('my title in de');
+    $article->setContent('my content in de');
+    $article->setTranslatableLocale('de_de');
+    $this->_em->persist($article);
+    $this->_em->flush();
+    
+Now then you load your article, it will be translated in used locale:
+
+    $article = $em->getRepository('Entities\Article')->find(1/* id of article */);
+    echo $article->getTitle();
+    // prints: "my title in en"
+    
+All translations can be loaded by TranslationRepository:
+
+    $repository = $em->getRepository('DoctrineExtensions\Translatable\Entity\Translation');
+    $translations = $repository->findTranslations($article);
+    // $translations contains:
+    Array (
+        [de_de] => Array
+            (
+                [title] => my title in de
+                [content] => my content in de
+            )
+    
+        [en_us] => Array
+            (
+                [title] => my title in en
+                [content] => my content in en
+            )
+    )
+    

+ 50 - 43
lib/DoctrineExtensions/Translatable/Repository/TranslationRepository.php

@@ -3,7 +3,9 @@
 namespace DoctrineExtensions\Translatable\Repository;
 
 use Doctrine\ORM\EntityRepository,
-    Doctrine\ORM\Query;
+    Doctrine\ORM\Query,
+    DoctrineExtensions\Translatable\Exception,
+    DoctrineExtensions\Translatable\Translatable;
 
 /**
  * The TranslationRepository has some useful functions
@@ -15,47 +17,52 @@ use Doctrine\ORM\EntityRepository,
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class TranslationRepository extends EntityRepository
-{
-    public function findTranslation($foreignKey, $field, $locale, $entity)
-    {
-    	$qb = $this->createQueryBuilder('trans');
-    	$qb->where(
-    	    'trans.foreignKey = :foreignKey',
-            'trans.locale = :locale',
-            'trans.field = :field',
-            'trans.entity = :entity'
-    	);
-    	$q = $qb->getQuery();
-    	$result = $q->execute(
-            compact('field', 'locale', 'foreignKey', 'entity'),
-            Query::HYDRATE_OBJECT
-    	);
-    	if ($result && is_array($result) && count($result)) {
-    		return array_shift($result);
-    	}
-    	return null;
-    }
-    
-    public function findFieldTranslation($foreignKey, $field, $locale, $entity)
-    {
-    	$qb = $this->createQueryBuilder('trans');
-        $qb->select('trans.content')
-            ->where(
-            'trans.foreignKey = :foreignKey',
-            'trans.locale = :locale',
-            'trans.field = :field',
-            'trans.entity = :entity'
-        );
-
-        $q = $qb->getQuery();
-        $result = $q->execute(
-            compact('field', 'locale', 'foreignKey', 'entity'),
-            Query::HYDRATE_ARRAY
-        );
-        if ($result && is_array($result) && count($result)) {
-            $result = array_shift($result);
-            return $result['content'];
+{	
+	/**
+	 * Loads all translations with all translatable
+	 * fields from the given entity
+	 * 
+	 * @param object $entity Must implement Translatable
+	 * @return array list of translations in locale groups
+	 */
+	public function findTranslations($entity)
+	{
+	    $result = array();
+        if ($entity instanceof Translatable) {
+	        $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);
+	        }
+	
+	        // @todo: add support for string type identifier also 
+	        if (!is_int($entityId)) {
+	            throw Exception::invalidIdentifierType($entityId);
+	        }
+	        
+	        $qb = $this->_em->createQueryBuilder();
+	        $qb->select('trans.content, trans.field, trans.locale')
+	            ->from($this->_entityName, 'trans')
+	            ->where('trans.foreignKey = :entityId', 'trans.entity = :entityClass')
+	            ->orderBy('trans.locale');
+	        $q = $qb->getQuery();
+	        $data = $q->execute(
+	            compact('entityId', 'entityClass'),
+	            Query::HYDRATE_ARRAY
+	        );
+	        
+	        if ($data && is_array($data) && count($data)) {
+	            foreach ($data as $row) {
+	            	$result[$row['locale']][$row['field']] = $row['content'];
+	            }
+	        }
         }
-        return null;
-    }
+        return $result;
+	}
 }