Ejemplo n.º 1
0
 /**
  * Handle an associative property field
  * @param string                              $name             - name of the field
  * @param \Zend\Code\Generator\ClassGenerator $cg               - The class generator object to attach to
  * @param ORMClassMetadata $ormClassMetaData - The ORM class meta data
  */
 private function handleAssocProperty($name, Generator\ClassGenerator &$cg, ORMClassMetadata $ormClassMetaData)
 {
     /** @var \Doctrine\ORM\Mapping\ClassMetaData $ormClassMetaData */
     $assocMapping = $ormClassMetaData->getAssociationMapping($name);
     $property = $this->createProperty($name);
     if ($assocMapping['type'] & $ormClassMetaData::TO_MANY) {
         // This is a collection (should be an Array)
         $property->setDocBlock('@var array $' . $name);
         $property->setDefaultValue([]);
         $paramType = self::PARAM_TYPE_RELATION_COLLECTION;
     } else {
         // This is a single relation
         $property->setDocBlock('@var ' . $assocMapping['targetEntity'] . ' $' . $name);
         $paramType = self::PARAM_TYPE_RELATION_SINGLE;
     }
     if (!$cg->hasProperty($name)) {
         $cg->addProperties([$property]);
         $cg->addMethods($this->getSetterMethods($cg, $name, $paramType, $assocMapping['targetEntity']));
     }
 }
 /**
  * @param DoctrineSqlFilter $sqlFilter
  * @param QuoteStrategy $quoteStrategy
  * @param ClassMetadata $targetEntity
  * @param string $targetTableAlias
  * @param string $targetEntityPropertyName
  * @return string
  * @throws InvalidQueryRewritingConstraintException
  * @throws \Exception
  */
 protected function getSqlForManyToOneAndOneToOneRelationsWithoutPropertyPath(DoctrineSqlFilter $sqlFilter, QuoteStrategy $quoteStrategy, ClassMetadata $targetEntity, $targetTableAlias, $targetEntityPropertyName)
 {
     $associationMapping = $targetEntity->getAssociationMapping($targetEntityPropertyName);
     $constraints = array();
     foreach ($associationMapping['joinColumns'] as $joinColumn) {
         $quotedColumnName = $quoteStrategy->getJoinColumnName($joinColumn, $targetEntity, $this->entityManager->getConnection()->getDatabasePlatform());
         $propertyPointer = $targetTableAlias . '.' . $quotedColumnName;
         $operandAlias = $this->operandDefinition;
         if (is_array($this->operandDefinition)) {
             $operandAlias = key($this->operandDefinition);
         }
         $currentReferencedOperandName = $operandAlias . $joinColumn['referencedColumnName'];
         if (is_object($this->operand)) {
             $operandMetadataInfo = $this->entityManager->getClassMetadata(TypeHandling::getTypeForValue($this->operand));
             $currentReferencedValueOfOperand = $operandMetadataInfo->getFieldValue($this->operand, $operandMetadataInfo->getFieldForColumn($joinColumn['referencedColumnName']));
             $this->setParameter($sqlFilter, $currentReferencedOperandName, $currentReferencedValueOfOperand, $associationMapping['type']);
         } elseif (is_array($this->operandDefinition)) {
             foreach ($this->operandDefinition as $operandIterator => $singleOperandValue) {
                 if (is_object($singleOperandValue)) {
                     $operandMetadataInfo = $this->entityManager->getClassMetadata(TypeHandling::getTypeForValue($singleOperandValue));
                     $currentReferencedValueOfOperand = $operandMetadataInfo->getFieldValue($singleOperandValue, $operandMetadataInfo->getFieldForColumn($joinColumn['referencedColumnName']));
                     $this->setParameter($sqlFilter, $operandIterator, $currentReferencedValueOfOperand, $associationMapping['type']);
                 } elseif ($singleOperandValue === NULL) {
                     $this->setParameter($sqlFilter, $operandIterator, NULL, $associationMapping['type']);
                 }
             }
         }
         $constraints[] = $this->getConstraintStringForSimpleProperty($sqlFilter, $propertyPointer, $currentReferencedOperandName);
     }
     return ' (' . implode(' ) AND ( ', $constraints) . ') ';
 }
 /**
  * Helper method to insert, remove or update translations entities associated with specified object
  *
  * @param \Doctrine\Common\Persistence\ObjectManager $objectManager
  * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $meta
  * @param \FSi\DoctrineExtensions\Translatable\Mapping\ClassMetadata $translatableMeta
  * @param object $object
  */
 protected function updateTranslations(ObjectManager $objectManager, ClassMetadata $meta, TranslatableClassMetadata $translatableMeta, $object)
 {
     $localeProperty = $translatableMeta->localeProperty;
     $propertyObserver = $this->getPropertyObserver($objectManager);
     $locale = $objectLocale = PropertyAccess::createPropertyAccessor()->getValue($object, $localeProperty);
     $objectLanguageChanged = !$propertyObserver->hasSavedValue($object, $localeProperty) && isset($objectLocale) || $propertyObserver->hasSavedValue($object, $localeProperty) && $propertyObserver->hasValueChanged($object, $localeProperty);
     if (!isset($locale)) {
         $locale = $this->getLocale();
     }
     $translatableProperties = $translatableMeta->getTranslatableProperties();
     foreach ($translatableProperties as $translation => $properties) {
         $translationEntity = $meta->getAssociationTargetClass($translation);
         $translationMeta = $objectManager->getClassMetadata($translationEntity);
         $translationLanguageField = $this->getTranslationLanguageField($objectManager, $translationMeta->name);
         $translationAssociation = $meta->getAssociationMapping($translation);
         $translations = $meta->getFieldValue($object, $translation);
         $currentTranslation = null;
         if (isset($translations)) {
             $currentTranslation = $this->findTranslation($translations, $translationMeta, $translationLanguageField, $locale);
         }
         $propertiesFound = false;
         foreach ($properties as $property => $translationField) {
             $propertyValue = PropertyAccess::createPropertyAccessor()->getValue($object, $property);
             if (isset($propertyValue)) {
                 $propertiesFound = true;
             }
             if ($objectLanguageChanged || !$propertyObserver->hasSavedValue($object, $property) && isset($propertyValue) || $propertyObserver->hasSavedValue($object, $property) && $propertyObserver->hasValueChanged($object, $property)) {
                 if (isset($propertyValue)) {
                     if (!isset($currentTranslation)) {
                         $currentTranslation = new $translationEntity();
                         $translationMeta->setFieldValue($currentTranslation, $translationLanguageField, $locale);
                         $translationMeta->setFieldValue($currentTranslation, $translationAssociation['mappedBy'], $object);
                         if (isset($translationAssociation['indexBy'])) {
                             $index = $translationMeta->getFieldValue($currentTranslation, $translationAssociation['indexBy']);
                             $translations[$index] = $currentTranslation;
                         } else {
                             $translations[] = $currentTranslation;
                         }
                         $objectManager->persist($currentTranslation);
                     }
                     $translationMeta->setFieldValue($currentTranslation, $translationField, $propertyValue);
                 } else {
                     if ($currentTranslation) {
                         $translationMeta->setFieldValue($currentTranslation, $translationField, null);
                     }
                 }
             }
         }
         if ($propertiesFound && !isset($locale)) {
             throw new Exception\RuntimeException('Neither object\'s locale nor the default locale was defined for translatable properties');
         }
         if (!$propertiesFound && isset($currentTranslation) && isset($objectLocale)) {
             $objectManager->remove($currentTranslation);
             if ($translations->contains($currentTranslation)) {
                 $translations->removeElement($currentTranslation);
             }
         }
     }
 }