/**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if ($metadata->isSingleValuedAssociation($associationName)) {
         $targetClassName = $metadata->getAssociationTargetClass($associationName);
         $groups = $this->groupingConfigProvider->getConfig($targetClassName)->get('groups');
         if (!empty($groups) && in_array(GroupingScope::GROUP_DICTIONARY, $groups)) {
             return true;
         }
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * @param \Faker\Generator $generator
  * @return array
  */
 public function guessColumnFormatters(\Faker\Generator $generator)
 {
     $formatters = array();
     $nameGuesser = new \Faker\Guesser\Name($generator);
     $columnTypeGuesser = new ColumnTypeGuesser($generator);
     foreach ($this->class->getFieldNames() as $fieldName) {
         if ($this->class->isIdentifier($fieldName) || !$this->class->hasField($fieldName)) {
             continue;
         }
         $size = isset($this->class->fieldMappings[$fieldName]['length']) ? $this->class->fieldMappings[$fieldName]['length'] : null;
         if ($formatter = $nameGuesser->guessFormat($fieldName, $size)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
         if ($formatter = $columnTypeGuesser->guessFormat($fieldName, $this->class)) {
             $formatters[$fieldName] = $formatter;
             continue;
         }
     }
     foreach ($this->class->getAssociationNames() as $assocName) {
         if ($this->class->isCollectionValuedAssociation($assocName)) {
             continue;
         }
         $relatedClass = $this->class->getAssociationTargetClass($assocName);
         $unique = $optional = false;
         $mappings = $this->class->getAssociationMappings();
         foreach ($mappings as $mapping) {
             if ($mapping['targetEntity'] == $relatedClass) {
                 if ($mapping['type'] == ClassMetadata::ONE_TO_ONE) {
                     $unique = true;
                     $optional = isset($mapping['joinColumns'][0]['nullable']) ? $mapping['joinColumns'][0]['nullable'] : false;
                     break;
                 }
             }
         }
         $index = 0;
         $formatters[$assocName] = function ($inserted) use($relatedClass, &$index, $unique, $optional) {
             if (isset($inserted[$relatedClass])) {
                 if ($unique) {
                     $related = null;
                     if (isset($inserted[$relatedClass][$index]) || !$optional) {
                         $related = $inserted[$relatedClass][$index];
                     }
                     $index++;
                     return $related;
                 }
                 return $inserted[$relatedClass][mt_rand(0, count($inserted[$relatedClass]) - 1)];
             }
             return null;
         };
     }
     return $formatters;
 }
Ejemplo n.º 3
0
 /**
  * @param ClassMetadata $classMetadata
  * @param string        $associationName
  *
  * @return AssociationMetadata
  */
 public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
 {
     $targetClass = $classMetadata->getAssociationTargetClass($associationName);
     $associationMetadata = new AssociationMetadata();
     $associationMetadata->setName($associationName);
     $associationMetadata->setTargetClassName($targetClass);
     $associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
     $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
     $targetIdFields = $targetMetadata->getIdentifierFieldNames();
     if (count($targetIdFields) === 1) {
         $associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
     } else {
         $associationMetadata->setDataType(DataType::STRING);
     }
     return $associationMetadata;
 }
Ejemplo n.º 4
0
 /**
  * @param ClassMetadata $meta
  * @param object $entity
  * @param string $field
  * @return bool|object
  */
 private function getRelation(ClassMetadata $meta, $entity, $field)
 {
     if (!$meta->hasAssociation($field) || !$meta->isSingleValuedAssociation($field)) {
         return FALSE;
     }
     // todo: allow access using property or method
     $relation = $meta->getFieldValue($entity, $field);
     if ($relation instanceof Collection) {
         return FALSE;
     }
     if ($relation === NULL) {
         $class = $meta->getAssociationTargetClass($field);
         $relationMeta = $this->mapper->getEntityManager()->getClassMetadata($class);
         $relation = $relationMeta->newInstance();
         $meta->setFieldValue($entity, $field, $relation);
     }
     return $relation;
 }
Ejemplo n.º 5
0
 /**
  * @param ClassMetadata $classMetadata
  * @param string        $associationName
  *
  * @return AssociationMetadata
  */
 public function createAssociationMetadata(ClassMetadata $classMetadata, $associationName)
 {
     $targetClass = $classMetadata->getAssociationTargetClass($associationName);
     $associationMetadata = new AssociationMetadata();
     $associationMetadata->setName($associationName);
     $associationMetadata->setTargetClassName($targetClass);
     $associationMetadata->setIsCollection($classMetadata->isCollectionValuedAssociation($associationName));
     $targetMetadata = $this->doctrineHelper->getEntityMetadataForClass($targetClass);
     $targetIdFields = $targetMetadata->getIdentifierFieldNames();
     if (count($targetIdFields) === 1) {
         $associationMetadata->setDataType($targetMetadata->getTypeOfField(reset($targetIdFields)));
     } else {
         $associationMetadata->setDataType(DataType::STRING);
     }
     if ($targetMetadata->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
         $associationMetadata->setAcceptableTargetClassNames($targetMetadata->subClasses);
     } else {
         $associationMetadata->addAcceptableTargetClassName($targetClass);
     }
     return $associationMetadata;
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 protected function hasAssociationConfigs(ClassMetadata $metadata, $associationName)
 {
     if ($this->isExtendField($metadata->name, $associationName)) {
         return false;
     }
     // check for default field of oneToMany or manyToMany relation
     if (strpos($associationName, ExtendConfigDumper::DEFAULT_PREFIX) === 0) {
         $guessedName = substr($associationName, strlen(ExtendConfigDumper::DEFAULT_PREFIX));
         if (!empty($guessedName) && $this->isExtendField($metadata->name, $guessedName)) {
             return false;
         }
     }
     // check for inverse side field of oneToMany relation
     $targetClass = $metadata->getAssociationTargetClass($associationName);
     $prefix = strtolower(ExtendHelper::getShortClassName($targetClass)) . '_';
     if (strpos($associationName, $prefix) === 0) {
         $guessedName = substr($associationName, strlen($prefix));
         if (!empty($guessedName) && $this->isExtendField($targetClass, $guessedName)) {
             return false;
         }
     }
     return parent::hasAssociationConfigs($metadata, $associationName);
 }
Ejemplo n.º 7
0
 /**
  * Returns the target class name of an association
  *
  * @param string $fieldName
  *
  * @return string
  */
 public function getAssociationTargetClass($fieldName)
 {
     return $this->metadata->getAssociationTargetClass($fieldName);
 }
Ejemplo n.º 8
0
 /**
  * @param FormBuilder $form
  * @param ClassMetadata $metaData
  * @param string $fieldName
  */
 public function createAssociationField($form, $metaData, $fieldName)
 {
     $formField = new SelectField(array());
     $formField->setLabel($this->translateName($fieldName));
     $formField->setName($fieldName);
     $mapping = $metaData->getAssociationMapping($fieldName);
     $required = false;
     $targetEntityName = $metaData->getAssociationTargetClass($fieldName);
     $targetEntity = $this->doctrineService->getRepository($targetEntityName);
     if ($metaData->isCollectionValuedAssociation($fieldName)) {
         $formField->setMultiple(true);
         $formField->setRequired(false);
     } else {
         foreach ($mapping['joinColumns'] as $joinColumn) {
             if (!$joinColumn['nullable']) {
                 $required = true;
                 break;
             }
         }
         $formField->setRequired($required);
     }
     $collection = static::entityToCollection($targetEntity->findAll(), !$formField->isMultiple());
     $formField->setCollection($collection);
     $form->addField($formField);
 }
 /**
  * @param ClassMetadata $metadata
  * @return JavaScript\Metadata
  */
 private function convertMetadata(ClassMetadata $metadata)
 {
     $meta = new JavaScript\Metadata();
     $meta->originalName = $metadata->getName();
     $meta->namespace = str_replace('\\', '_', $metadata->getReflectionClass()->getNamespaceName());
     $meta->functionName = $metadata->getReflectionClass()->getShortName();
     $parent = $metadata->getReflectionClass()->getParentClass();
     $meta->superFunctionName = $parent ? $parent->getShortName() : 'DBEntity';
     // Convert fields.
     foreach ($metadata->getFieldNames() as $fieldName) {
         $field = new JavaScript\Field();
         $field->name = $fieldName;
         $field->methodName = ucfirst($fieldName);
         $field->type = $this->convertDoctrineType($metadata->getTypeOfField($fieldName));
         $field->isIdentifier = $metadata->isIdentifier($fieldName);
         $meta->fields[] = $field;
     }
     // Convert associations.
     foreach ($metadata->getAssociationNames() as $assocName) {
         $assoc = new JavaScript\Association();
         $assoc->name = $assocName;
         $assoc->methodName = ucfirst($assocName);
         $assoc->isCollection = $metadata->isCollectionValuedAssociation($assocName);
         $assoc->isSingle = $metadata->isSingleValuedAssociation($assocName);
         $assoc->singleName = Inflector::singularize($assoc->methodName);
         $assoc->invertedField = $metadata->getAssociationMappedByTargetField($assocName);
         $targetClass = new \ReflectionClass($metadata->getAssociationTargetClass($assocName));
         $assoc->singleType = $targetClass->getShortName();
         $assoc->type = $assoc->singleType . ($assoc->isCollection ? '[]' : '');
         $meta->associations[] = $assoc;
     }
     return $meta;
 }
 /**
  * Set entity property value according to meta.
  *
  * @param ClassMetadata $meta
  * @param Entity        $entity
  * @param string        $name
  * @param string        $value
  *
  * @return Entity
  */
 private function setProperty(ClassMetadata $meta, Entity &$entity, $name, $value)
 {
     if ($meta->hasField($name) && !$meta->isIdentifier($name)) {
         $meta->setFieldValue($entity, $name, $value);
     } elseif ($meta->hasAssociation($name)) {
         // We have a single value and there is only one column in association
         if (!is_array($value) && !is_object($value) && $meta->isAssociationWithSingleJoinColumn($name)) {
             $id = [$meta->associationMappings[$name]['joinColumns'][0]['referencedColumnName'] => $value];
             $di = Di::getInstance();
             $linkedEntity = $di->em->find($meta->getAssociationTargetClass($name), $id);
             if (is_null($linkedEntity)) {
                 throw new ClientException('Entity not found for nested entity ' . json_encode(['name' => $name]), ClientException::CODE_NOT_FOUND);
             } else {
                 $meta->setFieldValue($entity, $name, $linkedEntity);
             }
         } else {
             throw new ServerException('Unhandled association type for field ' . $name . ' on ' . $meta->name, ServerException::CODE_NOT_IMPLEMENTED);
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     $isMainEntityIncluded = $this->isIncludedByChannels($metadata->getName());
     $isRelationIncluded = $this->isIncludedByChannels($metadata->getAssociationTargetClass($associationName));
     return !($isMainEntityIncluded && $isRelationIncluded);
 }
Ejemplo n.º 12
0
 /**
  * Set selectFields[], joins[] and allFields[] for associations.
  *
  * @param array         $fields   Association field array
  * @param int           $i        Numeric key
  * @param ClassMetadata $metadata A ClassMetadata instance
  *
  * @return DatatableData
  */
 private function setAssociations($fields, $i, $metadata)
 {
     $field = $fields[$i];
     if ($metadata->hasAssociation($field) === true) {
         $targetClass = $metadata->getAssociationTargetClass($field);
         $targetMeta = $this->em->getClassMetadata($targetClass);
         $targetTableName = $targetMeta->getTableName();
         $targetIdentifiers = $targetMeta->getIdentifierFieldNames();
         $targetRootIdentifier = array_shift($targetIdentifiers);
         if (!array_key_exists($targetTableName, $this->selectFields)) {
             $this->selectFields[$targetTableName][] = $targetRootIdentifier;
             $this->addJoin(array('source' => $metadata->getTableName() . '.' . $field, 'target' => $targetTableName));
         }
         $i++;
         $this->setAssociations($fields, $i, $targetMeta);
     } else {
         $targetTableName = $metadata->getTableName();
         $targetIdentifiers = $metadata->getIdentifierFieldNames();
         $targetRootIdentifier = array_shift($targetIdentifiers);
         if ($field !== $targetRootIdentifier) {
             array_push($this->selectFields[$targetTableName], $field);
         }
         $this->allFields[] = $targetTableName . '.' . $field;
     }
     return $this;
 }
Ejemplo n.º 13
0
 /**
  * @param ClassMetadata $metaData
  * @param object $entity
  * @param string $key
  * @param mixed $value
  */
 private function decodeAssociationSingle($metaData, $entity, $key, $value)
 {
     if ($value == '') {
         $value = null;
     } else {
         $targetEntityName = $metaData->getAssociationTargetClass($key);
         $value = $this->doctrineService->getRepository($targetEntityName)->findOneBy(['id' => $value]);
     }
     $methodName = 'set' . ucfirst($key);
     $entity->{$methodName}($value);
 }
 /**
  * @param ConfigContext $context
  * @param array         $fieldConfig
  * @param ClassMetadata $metadata
  * @param string        $fieldName
  * @param string        $rootEntityClass
  * @param string        $fieldPath
  */
 protected function setFieldCustomizationHandler(ConfigContext $context, array &$fieldConfig, ClassMetadata $metadata, $fieldName, $rootEntityClass, $fieldPath)
 {
     if (isset($fieldConfig[ConfigUtil::FIELDS]) && is_array($fieldConfig[ConfigUtil::FIELDS]) && $metadata->hasAssociation($fieldName)) {
         if (!isset($definition[ConfigUtil::POST_SERIALIZE])) {
             $fieldConfig[ConfigUtil::POST_SERIALIZE] = $this->getCustomizationHandler($context, $rootEntityClass, $fieldPath, $metadata->getAssociationTargetClass($fieldName));
         }
         $this->processFields($context, $fieldConfig[ConfigUtil::FIELDS], $rootEntityClass, $metadata, $fieldPath);
     }
 }