瀏覽代碼

translation entity abstraction through reflection

gediminasm 14 年之前
父節點
當前提交
46beef278a

+ 13 - 6
lib/DoctrineExtensions/Translatable/TranslationListener.php

@@ -204,11 +204,13 @@ class TranslationListener implements EventSubscriber
             $entityClassMetadata = $em->getClassMetadata($entityClass);
             // there should be single identifier
             $identifierField = $entityClassMetadata->getSingleIdentifierFieldName();
+            $translationClassMetadata = $em->getClassMetadata($this->getTranslationClass($entity));
         	if (array_key_exists($oid, $this->_pendingTranslationInserts)) {
                 // load the pending translations without key
         		$translations = $this->_pendingTranslationInserts[$oid];
         		foreach ($translations as $translation) {
-	                $translation->setForeignKey(
+	                $translationClassMetadata->getReflectionProperty('foreignKey')->setValue(
+                        $translation,
                         $entityClassMetadata->getReflectionProperty($identifierField)->getValue($entity)
                     );
 	                $this->_insertTranslationRecord($em, $translation);
@@ -326,15 +328,20 @@ class TranslationListener implements EventSubscriber
             // create new translation
             if (!$translation) {
                 $translation = new $translationClass();
-                $translation->setLocale($locale);
-	            $translation->setField($field);
-	            $translation->setEntity($entityClass);
-	            $translation->setForeignKey($entityId);
+                $translationMetadata->getReflectionProperty('locale')
+                    ->setValue($translation, $locale);
+                $translationMetadata->getReflectionProperty('field')
+                    ->setValue($translation, $field);
+                $translationMetadata->getReflectionProperty('entity')
+                    ->setValue($translation, $entityClass);
+                $translationMetadata->getReflectionProperty('foreignKey')
+                    ->setValue($translation, $entityId);
 	            $scheduleUpdate = !$isInsert;
             }
             
             // set the translated field, take value using reflection
-            $translation->setContent($entityClassMetadata->getReflectionProperty($field)->getValue($entity));
+            $translationMetadata->getReflectionProperty('content')
+                    ->setValue($translation, $entityClassMetadata->getReflectionProperty($field)->getValue($entity));
             if ($scheduleUpdate && $uow->hasPendingInsertions()) {
                 // need to shedule new Translation insert to avoid query on pending insert
                 $this->_pendingTranslationUpdates[] = $translation;

+ 0 - 110
tests/DoctrineExtensions/Translatable/EntityTranslationTableTest.php

@@ -188,114 +188,4 @@ class PersonTranslation
      * @Column(name="content", type="text", nullable=true)
      */
     private $content;
-    
-    /**
-     * Get id
-     *
-     * @return integer $id
-     */
-    public function getId()
-    {
-        return $this->id;
-    }
-
-    /**
-     * Set locale
-     *
-     * @param string $locale
-     */
-    public function setLocale($locale)
-    {
-        $this->locale = $locale;
-    }
-
-    /**
-     * Get locale
-     *
-     * @return string $locale
-     */
-    public function getLocale()
-    {
-        return $this->locale;
-    }
-
-    /**
-     * Set field
-     *
-     * @param string $field
-     */
-    public function setField($field)
-    {
-        $this->field = $field;
-    }
-
-    /**
-     * Get field
-     *
-     * @return string $field
-     */
-    public function getField()
-    {
-        return $this->field;
-    }
-
-    /**
-     * Set entity
-     *
-     * @param string $entity
-     */
-    public function setEntity($entity)
-    {
-        $this->entity = $entity;
-    }
-
-    /**
-     * Get entity
-     *
-     * @return string $entity
-     */
-    public function getEntity()
-    {
-        return $this->entity;
-    }
-    
-    /**
-     * Set foreignKey
-     *
-     * @param string $foreignKey
-     */
-    public function setForeignKey($foreignKey)
-    {
-        $this->foreignKey = $foreignKey;
-    }
-
-    /**
-     * Get foreignKey
-     *
-     * @return string $foreignKey
-     */
-    public function getForeignKey()
-    {
-        return $this->foreignKey;
-    }
-    
-    /**
-     * Set content
-     *
-     * @param text $content
-     */
-    public function setContent($content)
-    {
-        $this->content = $content;
-    }
-
-    /**
-     * Get content
-     *
-     * @return text $content
-     */
-    public function getContent()
-    {
-        return $this->content;
-    }
 }