Example #1
0
 /**
  * Creates the translation for entity being flushed
  * 
  * @param EntityManager $em
  * @param object $entity
  * @param boolean $isInsert
  * @throws Translatable\Exception if locale is not valid, or
  *      primary key is composite, missing or invalid
  * @return void
  */
 protected function _handleTranslatableEntityUpdate(EntityManager $em, $entity, $isInsert)
 {
     $entityClass = get_class($entity);
     // no need cache, metadata is loaded only once in MetadataFactoryClass
     $translationMetadata = $em->getClassMetadata($this->getTranslationClass($entityClass));
     $meta = $em->getClassMetadata($entityClass);
     // check for the availability of the primary key
     $entityId = $meta->getIdentifierValues($entity);
     if (count($entityId) == 1 && current($entityId)) {
         $entityId = current($entityId);
     } elseif ($isInsert) {
         $entityId = null;
     } else {
         throw Exception::singleIdentifierRequired($entityClass);
     }
     // load the currently used locale
     $locale = strtolower($this->getTranslatableLocale($entity, $meta));
     $this->_validateLocale($locale);
     $uow = $em->getUnitOfWork();
     $translationClass = $this->getTranslationClass($entityClass);
     $config = $this->getConfiguration($em, $entityClass);
     $translatableFields = $config['fields'];
     foreach ($translatableFields as $field) {
         $translation = null;
         $scheduleUpdate = false;
         // check if translation allready is created
         if (!$isInsert && !$uow->hasPendingInsertions()) {
             $translation = $this->_findTranslation($em, $entityId, $entityClass, $locale, $field);
         }
         // create new translation
         if (!$translation) {
             $translation = new $translationClass();
             $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
         $translationMetadata->getReflectionProperty('content')->setValue($translation, $meta->getReflectionProperty($field)->getValue($entity));
         if ($scheduleUpdate && $uow->hasPendingInsertions()) {
             // need to shedule new Translation insert to avoid query on pending insert
             $this->_pendingTranslationUpdates[] = $translation;
         } elseif ($isInsert && is_null($entityId)) {
             // if we do not have the primary key yet available
             // keep this translation in memory to insert it later with foreign key
             $this->_pendingTranslationInserts[spl_object_hash($entity)][$field] = $translation;
         } else {
             // persist and compute change set for translation
             $em->persist($translation);
             $uow->computeChangeSet($translationMetadata, $translation);
         }
     }
     // check if we have default translation and need to reset the translation
     if (!$isInsert && strlen($this->_defaultLocale)) {
         $this->_validateLocale($this->_defaultLocale);
         $changeSet = $uow->getEntityChangeSet($entity);
         $needsUpdate = false;
         foreach ($changeSet as $field => $changes) {
             if (in_array($field, $translatableFields)) {
                 if ($locale != $this->_defaultLocale && strlen($changes[0])) {
                     $meta->getReflectionProperty($field)->setValue($entity, $changes[0]);
                     $needsUpdate = true;
                 }
             }
         }
         if ($needsUpdate) {
             $uow->recomputeSingleEntityChangeSet($meta, $entity);
         }
     }
 }