Ejemplo n.º 1
0
 /** {@inheritdoc} */
 public function getAssociationMapping($name)
 {
     if (!$this->classMetadata->hasAssociation($name)) {
         throw new MetadataException("Association '{$name}' not found in class '{$this->getClass()}'.");
     }
     return $this->classMetadata->getAssociationMapping($name);
 }
Ejemplo n.º 2
0
 public function test_custom_region_gets_set_on_the_meta_data_on_build()
 {
     $cache = $this->factory('READ_ONLY', 'custom_region');
     $cache->build();
     $mapping = $this->metadata->getAssociationMapping($this->field);
     $this->assertEquals('custom_region', $mapping['cache']['region']);
 }
Ejemplo n.º 3
0
 public function testClassMetadataInstanceSerialization()
 {
     $cm = new ClassMetadata('Doctrine\\Tests\\Models\\CMS\\CmsUser');
     // Test initial state
     $this->assertTrue(count($cm->getReflectionProperties()) == 0);
     $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
     $this->assertEquals('Doctrine\\Tests\\Models\\CMS\\CmsUser', $cm->name);
     $this->assertEquals('Doctrine\\Tests\\Models\\CMS\\CmsUser', $cm->rootEntityName);
     $this->assertEquals(array(), $cm->subClasses);
     $this->assertEquals(array(), $cm->parentClasses);
     // Customize state
     $cm->setSubclasses(array("One", "Two", "Three"));
     $cm->setParentClasses(array("UserParent"));
     $cm->setCustomRepositoryClass("UserRepository");
     $cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
     $cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'Bar', 'mappedBy' => 'foo'));
     $this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
     $this->assertEquals(1, count($cm->associationMappings));
     $serialized = serialize($cm);
     $cm = unserialize($serialized);
     // Check state
     $this->assertTrue(count($cm->getReflectionProperties()) > 0);
     $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
     $this->assertEquals('Doctrine\\Tests\\Models\\CMS\\CmsUser', $cm->name);
     $this->assertEquals('UserParent', $cm->rootEntityName);
     $this->assertEquals(array('One', 'Two', 'Three'), $cm->subClasses);
     $this->assertEquals(array('UserParent'), $cm->parentClasses);
     $this->assertEquals('UserRepository', $cm->getCustomRepositoryClass());
     $this->assertEquals(array('name' => 'disc', 'type' => 'integer'), $cm->discriminatorColumn);
     $this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
     $this->assertEquals(1, count($cm->associationMappings));
     $oneOneMapping = $cm->getAssociationMapping('phonenumbers');
     $this->assertEquals('phonenumbers', $oneOneMapping->getSourceFieldName());
     $this->assertEquals('Doctrine\\Tests\\Models\\CMS\\Bar', $oneOneMapping->getTargetEntityName());
 }
Ejemplo n.º 4
0
 /**
  * @param string $association
  * @return array
  * @throws InvalidAssociationException
  */
 protected function getAssociationMapping($association)
 {
     try {
         return $this->metadata->getAssociationMapping($association);
     } catch (MappingException $e) {
         throw InvalidAssociationException::associationNotExist($this->entity, $association, $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     $mapping = $metadata->getAssociationMapping($associationName);
     if (!$mapping['isOwningSide'] || !($mapping['type'] & ClassMetadata::TO_ONE)) {
         return false;
     }
     return $associationName === 'workflowItem' || $associationName === 'workflowStep';
 }
Ejemplo n.º 6
0
 /**
  * Gibt das Property für eine Objekt-Fremd-Beziehung zurück
  *
  * dies ist z.B. bei der ManyToOne Seite möglich
  * @return Psc\CMS\EntityPropertyMeta
  */
 public function getForeignPropertyMeta(EntityMeta $foreignEntity)
 {
     throw new \Psc\Code\NotImplementedException();
     $foreignField = \Psc\Inflector::create()->propertyName($foreignEntity->getGClass()->getClassName());
     //var_dump($foreignEntity->getClassMetadata()->associationMappings);
     //var_dump($this->classMetadata->associationMappings);
     //var_dump($this->classMetadata->getFieldForColumn($foreignField));
     $mapping = $this->classMetadata->getAssociationMapping();
     //var_dump($mapping);
     return $this->getPropertyMeta($mapping);
 }
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if ($metadata->name !== 'Oro\\Bundle\\AttachmentBundle\\Entity\\Attachment') {
         return false;
     }
     $mapping = $metadata->getAssociationMapping($associationName);
     if (!$mapping['isOwningSide'] || !($mapping['type'] & ClassMetadata::MANY_TO_ONE)) {
         return false;
     }
     return $associationName === ExtendHelper::buildAssociationName($mapping['targetEntity']);
 }
Ejemplo n.º 8
0
 public static function getAssociationMapping(ClassMetadata $classMetadata, $name, $type = NULL)
 {
     if (!$classMetadata->hasAssociation($name)) {
         return NULL;
     }
     $mapping = $classMetadata->getAssociationMapping($name);
     $type = $type ? (array) $type : NULL;
     if ($type === NULL || in_array($mapping['type'], $type, TRUE)) {
         return $mapping;
     }
     return NULL;
 }
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if ($metadata->name !== 'Oro\\Bundle\\TrackingBundle\\Entity\\TrackingVisit') {
         return false;
     }
     $mapping = $metadata->getAssociationMapping($associationName);
     if (!$mapping['isOwningSide'] || !($mapping['type'] & ClassMetadata::MANY_TO_ONE)) {
         return false;
     }
     $identifierEventAssociationName = ExtendHelper::buildAssociationName($mapping['targetEntity'], IdentifierEventExtension::ASSOCIATION_KIND);
     return $associationName === $identifierEventAssociationName;
 }
Ejemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function isIgnoredRelation(ClassMetadata $metadata, $associationName)
 {
     if (!$this->configManager->hasConfig($metadata->name, $associationName)) {
         return false;
     }
     $groups = $this->configManager->getEntityConfig('grouping', $metadata->name)->get('groups');
     if (empty($groups) || !in_array(ActivityScope::GROUP_ACTIVITY, $groups, true)) {
         return false;
     }
     $mapping = $metadata->getAssociationMapping($associationName);
     if (!$mapping['isOwningSide'] || !($mapping['type'] & ClassMetadata::MANY_TO_MANY)) {
         return false;
     }
     $activityAssociationName = ExtendHelper::buildAssociationName($mapping['targetEntity'], ActivityScope::ASSOCIATION_KIND);
     return $associationName === $activityAssociationName;
 }
Ejemplo n.º 11
0
 /**
  * Merge DoctrineClassMetadata and DataAuditClassMetadata
  * @param  DoctrineClassMetadata $doctrineClassMetadata
  * @return null|ClassMetadata
  * @throws \InvalidArgumentException
  */
 public function extendLoadMetadataForClass(DoctrineClassMetadata $doctrineClassMetadata)
 {
     if ($doctrineClassMetadata->isMappedSuperclass || !($classMetadata = $this->loadMetadataForClass($doctrineClassMetadata->getReflectionClass()))) {
         return null;
     }
     /** @var $property PropertyMetadata */
     foreach ($classMetadata->propertyMetadata as $name => $property) {
         if ($doctrineClassMetadata->isInheritedField($name) || isset($doctrineClassMetadata->associationMappings[$property->name]['inherited'])) {
             unset($classMetadata->propertyMetadata[$name]);
             continue;
         }
         if ($doctrineClassMetadata->isCollectionValuedAssociation($name)) {
             $property->isCollection = true;
             $targetMapping = $doctrineClassMetadata->getAssociationMapping($name);
             if (!method_exists($targetMapping['targetEntity'], $property->method)) {
                 throw new \InvalidArgumentException(sprintf("Method %s in Class %s is not defined. Class must implement " . "a method '__toString' or configure getMethod with Versioned annotation", $property->method, $targetMapping['targetEntity']));
             }
         }
     }
     return $classMetadata;
 }
Ejemplo n.º 12
0
 private function getAssociationMetadata(ClassMetadata $meta, $associationName)
 {
     if (!$meta->hasAssociation($associationName)) {
         return FALSE;
     }
     $association = $meta->getAssociationMapping($associationName);
     if (!$association & ClassMetadata::TO_ONE) {
         return FALSE;
     }
     return $association;
 }
Ejemplo n.º 13
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);
 }
Ejemplo n.º 14
0
 /**
  * @param EntityMetadata $entityMetadata
  * @param ClassMetadata $classMetadata
  */
 protected function addDoctrineAssociations(EntityMetadata $entityMetadata, ClassMetadata $classMetadata)
 {
     $associations = array_diff($classMetadata->getAssociationNames(), $this->getDeletedFields($classMetadata->name));
     foreach ($associations as $fieldName) {
         $fieldMetadata = $this->metadataFactory->createFieldMetadata(['field_name' => $fieldName], $classMetadata->getAssociationMapping($fieldName));
         $entityMetadata->addFieldMetadata($fieldMetadata);
     }
 }
 private function getFromClause(ClassMetadata $metadataActivity, ClassMetadata $metadataPerson)
 {
     $associationMapping = $metadataActivity->getAssociationMapping('person');
     return $metadataActivity->getTableName() . ' JOIN ' . $metadataPerson->getTableName() . ' ON ' . $metadataPerson->getTableName() . '.' . $associationMapping['joinColumns'][0]['referencedColumnName'] . ' = ' . $associationMapping['joinColumns'][0]['name'];
 }
Ejemplo n.º 16
0
 /**
  * @param ClassMetadata $metaData
  * @param $fieldName
  *
  * @return array
  *
  * @throws MappingException
  */
 protected function getAssociationTypeOptions(ClassMetadata $metaData, $fieldName)
 {
     $fieldInfo = $metaData->getAssociationMapping($fieldName);
     $data = ['type' => 'entity'];
     if ($fieldInfo['type'] === ClassMetadataInfo::MANY_TO_ONE || $fieldInfo['type'] === ClassMetadataInfo::ONE_TO_ONE) {
         $data['options'] = ['class' => $fieldInfo['targetEntity'], 'choice_label' => $fieldInfo['joinColumns'][0]['referencedColumnName']];
     }
     if ($fieldInfo['type'] === ClassMetadataInfo::MANY_TO_MANY) {
         $data['options'] = ['class' => $fieldInfo['targetEntity'], 'choice_label' => $fieldInfo['joinTable']['joinColumns'][0]['referencedColumnName'], 'multiple' => true];
     }
     return $data;
 }
Ejemplo n.º 17
0
 /**
  * Get mappings.
  *
  * @param ClassMetadata $metadata
  * @param array         $fields
  *
  * @return array
  * @throws MappingException
  */
 protected function getMappings(ClassMetadata $metadata, array $fields)
 {
     $mappings = array();
     foreach ($fields as $field) {
         try {
             // Gets the mapping of a regular field
             $mappings[$field] = $metadata->getFieldMapping($field);
         } catch (MappingException $e) {
             // Gets the mapping of an association
             $mappings[$field] = $metadata->getAssociationMapping($field);
         }
     }
     return $mappings;
 }
Ejemplo n.º 18
0
 /**
  * Gets a list of all indexed associations
  *
  * @param ClassMetadata $metadata
  *
  * @return array [field name => target field data-type, ...]
  */
 public function getIndexedAssociations(ClassMetadata $metadata)
 {
     $relations = [];
     $fieldNames = $metadata->getAssociationNames();
     foreach ($fieldNames as $fieldName) {
         $mapping = $metadata->getAssociationMapping($fieldName);
         if ($mapping['type'] & ClassMetadata::TO_ONE) {
             $targetMetadata = $this->getEntityMetadataForClass($mapping['targetEntity']);
             $targetIdFieldNames = $targetMetadata->getIdentifierFieldNames();
             if (count($targetIdFieldNames) === 1) {
                 $relations[$fieldName] = $targetMetadata->getTypeOfField(reset($targetIdFieldNames));
             }
         }
     }
     return $relations;
 }
 /**
  * Transform a class entityMetadata object to a more user-friendly array
  *
  * @param ClassMetadata $metaData
  * @return array
  * @throws \Exception
  */
 public function parseMetaDataToFieldArray(ClassMetadata $metaData)
 {
     $columns = array();
     foreach (array_keys($metaData->reflFields) as $fieldName) {
         if (array_key_exists($fieldName, $metaData->fieldMappings)) {
             $fieldData = $metaData->getFieldMapping($fieldName);
             $columns[$fieldName] = $fieldData;
         } elseif (array_key_exists($fieldName, $metaData->associationMappings)) {
             $fieldData = $metaData->getAssociationMapping($fieldName);
             $fieldData['associationType'] = $fieldData['type'];
             $fieldData['type'] = 'association';
             $columns[$fieldName] = $fieldData;
         } else {
             throw new \Exception(sprintf('Can\'t map %s in the %s Entity', $fieldName, $metaData->name));
         }
     }
     return $columns;
 }
Ejemplo n.º 20
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 \Doctrine\ORM\Mapping\ClassMetadata $ormClassMetaData - The ORM class meta data
  */
 private function handleAssocProperty($name, Generator\ClassGenerator &$cg, ORMClassMetadata $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(array());
         $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(array($property));
         $cg->addMethods($this->getSetterMethods($cg, $name, $paramType, $assocMapping['targetEntity']));
     }
 }
Ejemplo n.º 21
0
 /**
  * Maps ids of associations to references
  *
  * @param ClassMetadata $class
  * @param string        $field
  * @param mixed         $value
  */
 protected function mapValue(ClassMetadata $class, $field, &$value)
 {
     if (!$class->isSingleValuedAssociation($field)) {
         return;
     }
     $mapping = $class->getAssociationMapping($field);
     $value = $value ? $this->em->getReference($mapping['targetEntity'], $value) : null;
 }
Ejemplo n.º 22
0
 /**
  * @param ClassMetadata $targetEntityMetadata
  * @return array
  */
 protected function processIdentifier(ClassMetadata $targetEntityMetadata)
 {
     $identifierFields = $targetEntityMetadata->identifier;
     $this->identifierFields = array_filter($identifierFields, function ($value) use($targetEntityMetadata) {
         if (isset($targetEntityMetadata->associationMappings[$value])) {
             $associationMapping = $targetEntityMetadata->getAssociationMapping($value);
             return $this->wrappedEntity->getMetadata()->getName() != $associationMapping['targetEntity'];
         }
         return TRUE;
     });
     return $identifierFields;
 }
Ejemplo n.º 23
0
 public function getPropertyDoctrineAssociationMapping($property)
 {
     if ($this->orm_metadata->hasAssociation($property)) {
         return $this->orm_metadata->getAssociationMapping($property);
     }
 }
Ejemplo n.º 24
0
 /**
  * Returns the mapping of an association
  *
  * @param string $fieldName
  *
  * @return array
  */
 public function getAssociationMapping($fieldName)
 {
     return $this->metadata->getAssociationMapping($fieldName);
 }
Ejemplo n.º 25
0
 /**
  * @param ClassMetadata $objectMeta
  * @param string        $field
  * @param mixed         $value
  */
 protected function mapValue($om, ClassMetadata $objectMeta, $field, &$value)
 {
     if (!$objectMeta->isSingleValuedAssociation($field)) {
         return;
     }
     $mapping = $objectMeta->getAssociationMapping($field);
     $value = $value ? $om->getReference($mapping['targetEntity'], $value) : null;
 }
Ejemplo n.º 26
0
 /**
  * @param Builder\Metadata $meta
  * @param \Doctrine\ORM\Mapping\ClassMetadata $cm
  */
 private function loadMeta(Builder\Metadata $meta, \Doctrine\ORM\Mapping\ClassMetadata $cm)
 {
     $type = null;
     if ($cm->hasField($meta->name)) {
         $map = $cm->getFieldMapping($meta->name);
         $type = $map['type'];
         switch ($type) {
             case 'smallint':
             case 'bigint':
                 $type = 'integer';
                 break;
             default:
                 break;
         }
         if (!isset($map['nullable']) || $map['nullable'] === false && !isset($meta->conditions['required'])) {
             $meta->conditions['required'] = true;
         }
         if (isset($map['length']) && $map['length'] && !isset($meta->conditions['maxLenght'])) {
             $meta->conditions['maxLength'] = $map['length'];
         }
         if ($type === 'decimal' && isset($map['scale'])) {
             $type = 'float';
             $meta->custom['step'] = pow(10, -$map['scale']);
         }
     } elseif ($cm->hasAssociation($meta->name)) {
         $map = $cm->getAssociationMapping($meta->name);
         $type = $map['targetEntity'];
     }
     if (!$meta->type) {
         $meta->type = $type;
     }
 }
Ejemplo n.º 27
0
 /**
  * @param ClassMetadata $metaData
  * @param $fieldName
  *
  * @return array
  *
  * @throws MappingException
  */
 protected function getAssociationTypeOptions(ClassMetadata $metaData, $fieldName)
 {
     $fieldInfo = $metaData->getAssociationMapping($fieldName);
     $data = ['type' => 'entity'];
     if ($fieldInfo['type'] == 2) {
         $data['options'] = ['class' => $fieldInfo['targetEntity'], 'choice_label' => $fieldInfo['joinColumns'][0]['referencedColumnName']];
     }
     return $data;
 }