/** * @param bool $withRelations * * @dataProvider testDataProvider */ public function testBuildFormRegularGuesser($withRelations) { $entityName = 'Test\\Entity'; $this->doctrineHelperMock->expects($this->once())->method('getEntityIdentifierFieldNames')->with($entityName)->willReturn(['id']); $fields = [['name' => 'oneField', 'type' => 'string', 'label' => 'One field'], ['name' => 'anotherField', 'type' => 'string', 'label' => 'Another field']]; if ($withRelations) { $fields[] = ['name' => 'relField', 'relation_type' => 'ref-one', 'label' => 'Many to One field']; } $this->entityFieldMock->expects($this->once())->method('getFields')->willReturn($fields); $this->formConfigMock->expects($this->at(0))->method('getConfig')->with($entityName, 'oneField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'someField'), ['is_enabled' => false])); $this->formConfigMock->expects($this->at(1))->method('getConfig')->with($entityName, 'anotherField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'anotherField'), ['is_enabled' => true])); if ($withRelations) { $this->formConfigMock->expects($this->at(2))->method('getConfig')->with($entityName, 'relField')->willReturn(new Config(new FieldConfigId('form', $entityName, 'relField'), ['is_enabled' => true])); $this->translatorMock->expects($this->at(0))->method('trans')->with('oro.entity.form.entity_fields')->willReturn('Fields'); $this->translatorMock->expects($this->at(1))->method('trans')->with('oro.entity.form.entity_related')->willReturn('Relations'); } $form = $this->factory->create($this->type, null, ['entity' => $entityName, 'with_relations' => $withRelations]); $view = $form->createView(); $this->assertEquals('update_field_choice', $view->vars['full_name'], 'Failed asserting that field name is correct'); $this->assertNotEmpty($view->vars['configs']['component']); $this->assertEquals('entity-field-choice', $view->vars['configs']['component']); $this->assertEquals('update_field_choice', $form->getConfig()->getType()->getName(), 'Failed asserting that correct underlying type was used'); if ($withRelations) { $this->assertCount(2, $view->vars['choices'], 'Failed asserting that choices are grouped'); } else { $this->assertCount(1, $view->vars['choices'], 'Failed asserting that choices exists'); /** @var ChoiceView $choice */ $choice = reset($view->vars['choices']); $this->assertEquals('Another field', $choice->label); } }
/** * @see \Oro\Bundle\EntityBundle\Provider\EntityFieldProvider::getRelations * * @param string $entityName * @param bool $withEntityDetails * @param bool $applyExclusions * @param bool $translate * @return array */ public function getRelations($entityName, $withEntityDetails = false, $applyExclusions = true, $translate = true) { $args = func_get_args(); $cacheKey = implode(':', $args); if (!array_key_exists($cacheKey, $this->relationsCache)) { $this->relationsCache[$cacheKey] = $this->fieldProvider->getRelations($entityName, $withEntityDetails, $applyExclusions, $translate); } return $this->relationsCache[$cacheKey]; }
/** * @return array */ protected function getPropertyChoiceList() { $choices = []; if (!$this->entityClass) { return $choices; } $fields = $this->entityFieldProvider->getFields($this->entityClass); foreach ($fields as $field) { if (!in_array($field['type'], ['integer', 'string', 'smallint', 'decimal', 'bigint', 'text', 'money'])) { continue; } $choices[$field['name']] = $field['label'] ?: $field['name']; } return $choices; }
/** * Returns a list of choices * * @param string $entityName Entity name. Can be full class name or short form: Bundle:Entity. * @param bool $withRelations Indicates whether fields of related entities should be returned as well. * @param int $deepLevel The maximum deep level of related entities. * @param bool $lastDeepLevelRelations The maximum deep level of related entities. * @return array of entity fields * key = field name, value = ChoiceListItem */ protected function getChoices($entityName, $withRelations, $deepLevel = 0, $lastDeepLevelRelations = false) { $choiceFields = array(); $choiceRelations = array(); $isRelationsWithFields = false; $fields = $this->provider->getFields($entityName, $withRelations, true, $deepLevel, $lastDeepLevelRelations); foreach ($fields as $field) { $attributes = []; foreach ($field as $key => $val) { if (!in_array($key, ['name'])) { $attributes['data-' . str_replace('_', '-', $key)] = $val; } } if (!isset($field['related_entity_name'])) { $choiceFields[$field['name']] = new ChoiceListItem($field['label'], $attributes); } else { if (isset($field['related_entity_fields'])) { $isRelationsWithFields = true; $relatedFields = array(); foreach ($field['related_entity_fields'] as $relatedField) { $attributes = []; foreach ($relatedField as $key => $val) { if (!in_array($key, ['related_entity_fields'])) { $attributes['data-' . str_replace('_', '-', $key)] = $val; } } $relatedFields[sprintf('%s,%s::%s', $field['name'], $field['related_entity_name'], $relatedField['name'])] = new ChoiceListItem($relatedField['label'], $attributes); } $choiceRelations[$field['label']] = $relatedFields; } else { $choiceRelations[$field['name']] = new ChoiceListItem($field['label'], $attributes); } } } if (empty($choiceRelations)) { return $choiceFields; } $choices = array(); if (!empty($choiceFields)) { $choices[$this->translator->trans('oro.entity.form.entity_fields')] = $choiceFields; } if ($isRelationsWithFields) { $choices = array_merge($choices, $choiceRelations); } else { $choices[$this->translator->trans('oro.entity.form.entity_related')] = $choiceRelations; } return $choices; }
/** * Returns a list of choices * * @param string $entityName Entity name. Can be full class name or short form: Bundle:Entity. * @param bool $withRelations Indicates whether association fields should be returned as well. * @param bool $withVirtualFields Indicates whether virtual fields should be returned as well. * @return array of entity fields * key = field name, value = ChoiceListItem */ protected function getChoices($entityName, $withRelations, $withVirtualFields) { $choiceFields = []; $choiceRelations = []; $fields = $this->entityFieldProvider->getFields($entityName, $withRelations, $withVirtualFields, true); foreach ($fields as $field) { $attributes = []; foreach ($field as $key => $val) { if (!in_array($key, ['name', 'related_entity_fields'])) { $attributes['data-' . $key] = $val; } } if (!isset($field['relation_type'])) { $choiceFields[$field['name']] = new ChoiceListItem($field['label'], $attributes); } else { $choiceRelations[$field['name']] = new ChoiceListItem($field['label'], $attributes); } } if (empty($choiceRelations)) { return $choiceFields; } $choices = []; if (!empty($choiceFields)) { $choices[$this->translator->trans('oro.entity.form.entity_fields')] = $choiceFields; } $choices[$this->translator->trans('oro.entity.form.entity_related')] = $choiceRelations; return $choices; }
/** * {@inheritdoc} */ protected function isIgnoredRelation(ClassMetadata $metadata, $associationName) { // skip workflow and collection relations if ($this->isIgnoredInWorkflow($metadata, $associationName)) { return true; } return parent::isIgnoredRelation($metadata, $associationName); }
/** * {@inheritdoc} */ protected function isIgnoredRelation(ClassMetadata $metadata, $associationName) { // skip workflow and collection relations if ($this->isWorkflowField($associationName) || !$metadata->isSingleValuedAssociation($associationName)) { return true; } return parent::isIgnoredRelation($metadata, $associationName); }
/** * @param bool $hasConfig * @param bool $isEnabled * @param bool $isManyToOneExists * @param string $expectedType * @param array $expectedOptions * * @dataProvider guessTypeProvider */ public function testGuessType($hasConfig, $isEnabled, $isManyToOneExists, $expectedType, $expectedOptions) { $className = 'Test\\Entity'; $relatedEntityName = 'Test\\EntityRelated'; $property = 'subject'; $this->setupExtendHasConfig($className, $property, $hasConfig, ['is_deleted' => false, 'state' => ExtendScope::STATE_ACTIVE, 'target_entity' => $relatedEntityName]); $this->setupFormIsEnabled($className, $property, RelationType::TO_ONE, $isEnabled); if ($hasConfig && $isEnabled) { $this->fieldProviderMock->expects($this->once())->method('getFields')->with($className, true)->willReturn([['name' => 'subject', 'related_entity_name' => $relatedEntityName]]); } if ($isManyToOneExists) { $this->setupEntityConfig($className, $property); $this->guesser->addExtendTypeMapping(RelationType::TO_ONE, 'entity'); } $guess = $this->guesser->guessType($className, $property); $this->assertEquals($expectedType, $guess->getType(), 'Failed asserting type guessed correctly'); $this->assertEquals($expectedOptions, $guess->getOptions(), 'Failed asserting options guessed correctly'); }
/** * {@inheritdoc} */ protected function getOptions(ConfigInterface $extendConfig, FieldConfigId $fieldConfigId) { $options = parent::getOptions($extendConfig, $fieldConfigId); $className = $fieldConfigId->getClassName(); $fieldName = $fieldConfigId->getFieldName(); switch ($fieldConfigId->getFieldType()) { case RelationType::TO_ONE: if (empty($this->fieldList) && $this->fieldProvider) { $fieldList = $this->fieldProvider->getFields($className, true); foreach ($fieldList as $entityField) { $this->fieldList[$entityField['name']] = $entityField; } } $options['class'] = $this->fieldList[$fieldName]['related_entity_name']; break; } return $options; }
/** * @param array $expected * * @dataProvider getFieldsWithVirtualRelationsAndEnumsDataProvider */ public function testGetFieldsWithVirtualRelationsAndEnums(array $expected) { $className = 'Acme\\Entity\\Test'; $config = [$className => ['config' => ['label' => 'Test Label', 'plural_label' => 'Test Plural Label', 'icon' => 'icon-test'], 'fields' => ['field1' => ['type' => 'integer', 'identifier' => true, 'config' => ['label' => 'Field 1']]], 'relations' => ['rel1' => ['target_class' => 'Acme\\EnumValue1', 'type' => 'ref-one', 'config' => ['label' => 'Enum Field']], 'rel2' => ['target_class' => 'Acme\\EnumValue2', 'type' => 'ref-many', 'config' => ['label' => 'Multi Enum Field']]]]]; $this->prepare($config); $this->virtualFieldProvider->expects($this->once())->method('getVirtualFields')->with($className)->will($this->returnValue(['rel1', 'rel2'])); $this->virtualRelationProvider->expects($this->once())->method('getVirtualRelations')->with($className)->will($this->returnValue(['virtual_relation' => ['relation_type' => 'oneToMany', 'related_entity_name' => 'OtherEntity', 'query' => ['select' => ['select expression'], 'join' => ['join expression']]]])); $this->virtualFieldProvider->expects($this->exactly(2))->method('getVirtualFieldQuery')->will($this->returnValueMap([[$className, 'rel1', ['select' => ['return_type' => 'enum', 'filter_by_id' => true]]], [$className, 'rel2', ['select' => ['return_type' => 'multiEnum', 'filter_by_id' => true]]]])); $result = $this->provider->getFields('Acme:Test', true, true); $this->assertEquals($expected, $result); }
/** * @param string $entity * @return array */ public function getEntityContactInformationColumnsInfo($entity) { $fields = array(); $contactInformationFields = $this->getEntityContactInformationColumns($entity); $entityFields = $this->fieldProvider->getFields($entity, false, true); foreach ($entityFields as $entityField) { if (array_key_exists($entityField['name'], $contactInformationFields)) { $entityField['contact_information_type'] = $contactInformationFields[$entityField['name']]; $fields[] = $entityField; } } return $fields; }
/** * Finds detached properties in entity and reloads them from UnitOfWork * * @param object $entity doctrine entity * @param int $level maximum nesting level */ public function fixEntityAssociationFields($entity, $level = 0) { if ($level < 0) { return; } // we should use entityFieldProvider to get relations data to avoid deleted relations in result list $relations = $this->entityFieldProvider->getRelations(ClassUtils::getClass($entity)); if (!$relations) { return; } $propertyAccessor = PropertyAccess::createPropertyAccessor(); foreach ($relations as $associationMapping) { $fieldName = $associationMapping['name']; $value = $propertyAccessor->getValue($entity, $fieldName); if ($value && is_object($value)) { if ($value instanceof Collection) { $this->fixCollectionField($value, $level); } else { $this->fixEntityField($entity, $fieldName, $value, $level); } } } }
/** * @param array $expected * * @dataProvider relationsExpectedDataProvider */ public function testGetRelations(array $expected) { $this->prepareWithRelations(); $result = $this->provider->getRelations('Acme:Test', true); $this->assertEquals($expected, $result); }
/** * Returns source data for the given entity * * @param string $entityName Entity name. Can be full class name or short form: Bundle:Entity. * @param bool $withRelations Indicates whether association fields should be returned as well. * @param bool $withVirtualFields Indicates whether virtual fields should be returned as well. * @param bool $withUnidirectional Indicates whether Unidirectional association fields should be returned. * should be returned. * @return array */ protected function getData($entityName, $withRelations, $withVirtualFields, $withUnidirectional) { $fields = $this->entityFieldProvider->getFields($entityName, $withRelations, $withVirtualFields, true, $withUnidirectional); return $this->convertData($fields, $entityName, null); }