/** * @param ConfigInterface $sourceEntityConfig The 'extend' config of the source entity * @param string $targetEntityName * @param string $relationName * @param string $targetFieldName A field name is used to show related entity * @param array $options * @param string $fieldType The field type. By default the field type is manyToOne, * but you can specify another type if it is based on manyToOne. * In this case this type should be registered * in entity_extend.yml under underlying_types section * * @return string The relation key */ public function addManyToOneRelation(ConfigInterface $sourceEntityConfig, $targetEntityName, $relationName, $targetFieldName, $options = [], $fieldType = 'manyToOne') { $sourceEntityName = $sourceEntityConfig->getId()->getClassName(); $relationKey = ExtendHelper::buildRelationKey($sourceEntityName, $relationName, 'manyToOne', $targetEntityName); // add a relation field config if (!$this->configManager->hasConfigFieldModel($sourceEntityName, $relationName)) { $this->configManager->createConfigFieldModel($sourceEntityName, $relationName, $fieldType); $options['extend']['state'] = ExtendScope::STATE_NEW; } else { $configFieldModel = $this->configManager->getConfigFieldModel($sourceEntityName, $relationName); if ($configFieldModel->getType() !== $fieldType) { $this->configManager->changeFieldType($sourceEntityName, $relationName, $fieldType); } } $options['extend']['is_extend'] = true; $options['extend']['relation_key'] = $relationKey; $options['extend']['target_entity'] = $targetEntityName; $options['extend']['target_field'] = $targetFieldName; $this->updateFieldConfigs($sourceEntityName, $relationName, $options); // add relation to config $relations = $sourceEntityConfig->get('relation', false, []); if (!isset($relations[$relationKey])) { $relations[$relationKey] = ['assign' => false, 'field_id' => new FieldConfigId('extend', $sourceEntityName, $relationName, 'manyToOne'), 'owner' => true, 'target_entity' => $targetEntityName, 'target_field_id' => false]; $sourceEntityConfig->set('relation', $relations); $extendConfigProvider = $this->configManager->getProvider('extend'); $extendConfigProvider->persist($sourceEntityConfig); } return $relationKey; }
/** * @param ConfigInterface $entityConfig * @param string $targetEntityClassName * @param string $relationName */ protected function createOwnerRelation(ConfigInterface $entityConfig, $targetEntityClassName, $relationName) { $relationKey = ExtendHelper::buildRelationKey($entityConfig->getId()->getClassName(), $relationName, 'manyToOne', $this->ownershipMetadataProvider->getOrganizationClass()); if (!isset($entityConfig->get('relation')[$relationKey])) { $this->relationBuilder->addManyToOneRelation($entityConfig, $targetEntityClassName, $relationName, 'id', ['entity' => ['label' => 'oro.custom_entity.' . $relationName . '.label', 'description' => 'oro.custom_entity.' . $relationName . '.description'], 'view' => ['is_displayable' => false], 'form' => ['is_enabled' => false], 'dataaudit' => ['auditable' => true]]); } }
/** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventListener(FormEvents::POST_SUBMIT, function () use($options) { /** @var FieldConfigId $configId */ $configId = $options['config_id']; $relationKey = ExtendHelper::buildRelationKey($configId->getClassName(), $configId->getFieldName(), 'manyToOne', 'Oro\\Bundle\\AttachmentBundle\\Entity\\File'); /** @var Config $entityExtendConfig */ $entityExtendConfig = $this->extendConfigProvider->getConfig($configId->getClassName()); if ($this->isApplicable($entityExtendConfig, $relationKey)) { if ($entityExtendConfig->is('state', ExtendScope::STATE_ACTIVE)) { $entityExtendConfig->set('state', ExtendScope::STATE_UPDATE); $this->extendConfigProvider->persist($entityExtendConfig); $this->extendConfigProvider->flush(); } } }); }
/** * @param string $entityClass * @param string $activityClassName * * @return bool */ protected function isActivityAssociationEnabled($entityClass, $activityClassName) { $extendConfig = $this->extendConfigProvider->getConfig($activityClassName); $relations = $extendConfig->get('relation', false, []); $relationKey = ExtendHelper::buildRelationKey($activityClassName, ExtendHelper::buildAssociationName($entityClass, ActivityScope::ASSOCIATION_KIND), RelationType::MANY_TO_MANY, $entityClass); return isset($relations[$relationKey]); }
/** * @param ConfigInterface $fieldConfig */ protected function createSelfRelation(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $targetEntityClass = $fieldConfig->get('target_entity'); $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName()); $relationKey = ExtendHelper::buildRelationKey($selfFieldId->getClassName(), $selfFieldId->getFieldName(), $selfFieldId->getFieldType(), $targetEntityClass); /** * in case of oneToMany relation * automatically create target field (type: manyToOne) */ $targetFieldId = false; $owner = true; $targetOwner = false; if (in_array($selfFieldId->getFieldType(), RelationType::$toManyRelations)) { $classNameArray = explode('\\', $selfFieldId->getClassName()); $relationFieldName = strtolower(array_pop($classNameArray)) . '_' . $selfFieldId->getFieldName(); if ($selfFieldId->getFieldType() === RelationType::ONE_TO_MANY) { $owner = false; $targetOwner = true; } $targetFieldId = new FieldConfigId('extend', $targetEntityClass, $relationFieldName, ExtendHelper::getReverseRelationType($selfFieldId->getFieldType())); } $selfRelationConfig = ['assign' => false, 'field_id' => $selfFieldId, 'owner' => $owner, 'target_entity' => $targetEntityClass, 'target_field_id' => $targetFieldId]; if ($fieldConfig->has('cascade')) { $selfRelationConfig['cascade'] = $fieldConfig->get('cascade'); } $selfRelations = $selfConfig->get('relation') ?: []; $selfRelations[$relationKey] = $selfRelationConfig; $selfConfig->set('relation', $selfRelations); $this->extendConfigProvider->persist($selfConfig); $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass); $targetRelationConfig = ['assign' => false, 'field_id' => $targetFieldId, 'owner' => $targetOwner, 'target_entity' => $selfFieldId->getClassName(), 'target_field_id' => $selfFieldId]; $targetRelations = $targetConfig->get('relation') ?: []; $targetRelations[$relationKey] = $targetRelationConfig; $targetConfig->set('relation', $targetRelations); $fieldConfig->set('relation_key', $relationKey); $this->extendConfigProvider->persist($targetConfig); }
/** * @param string $entityClass * @param string $fieldName * @param string $label * @param string $description * @param string $targetEntity * @param string $targetField */ protected function addRelationField($entityClass, $fieldName, $label, $description, $targetEntity, $targetField) { $this->configManager->createConfigFieldModel($entityClass, $fieldName, 'manyToOne'); $entityConfigProvider = $this->configManager->getProvider('entity'); $entityFieldConfig = $entityConfigProvider->getConfig($entityClass, $fieldName); $entityFieldConfig->set('label', $label); $entityFieldConfig->set('description', $description); $extendConfigProvider = $this->configManager->getProvider('extend'); $extendFieldConfig = $extendConfigProvider->getConfig($entityClass, $fieldName); $extendFieldConfig->set('owner', ExtendScope::OWNER_CUSTOM); $extendFieldConfig->set('state', ExtendScope::STATE_NEW); $extendFieldConfig->set('is_extend', true); $extendFieldConfig->set('target_entity', $targetEntity); $extendFieldConfig->set('target_field', $targetField); $extendFieldConfig->set('relation_key', ExtendHelper::buildRelationKey($entityClass, $targetField, 'manyToOne', $targetEntity)); $formConfigProvider = $this->configManager->getProvider('form'); $formFieldConfig = $formConfigProvider->getConfig($entityClass, $fieldName); $formFieldConfig->set('is_enabled', false); $viewConfigProvider = $this->configManager->getProvider('view'); $viewFieldConfig = $viewConfigProvider->getConfig($entityClass, $fieldName); $viewFieldConfig->set('is_displayable', false); $importExportConfigProvider = $this->configManager->getProvider('importexport'); $importExportFieldConfig = $importExportConfigProvider->getConfig($entityClass, $fieldName); $importExportFieldConfig->set('excluded', true); }
public function testGetActivityActions() { $targetEntityClass = 'Test\\Entity'; $activity1Class = 'Test\\Activity1'; $activity2Class = 'Test\\Activity2'; $activity1RelationKey = ExtendHelper::buildRelationKey($activity1Class, ExtendHelper::buildAssociationName($targetEntityClass, ActivityScope::ASSOCIATION_KIND), 'manyToMany', $targetEntityClass); $activity2RelationKey = ExtendHelper::buildRelationKey($activity2Class, ExtendHelper::buildAssociationName($targetEntityClass, ActivityScope::ASSOCIATION_KIND), 'manyToMany', $targetEntityClass); $targetEntityActivityConfig = new Config(new EntityConfigId('activity', $targetEntityClass)); $targetEntityActivityConfig->set('activities', [$activity1Class, $activity2Class]); $activity1ExtendConfig = new Config(new EntityConfigId('extend', $activity1Class)); $activity1ExtendConfig->set('relation', [$activity1RelationKey => []]); $activity1EntityConfig = new Config(new EntityConfigId('entity', $activity1Class)); $activity1EntityConfig->set('plural_label', 'lbl.activity1'); $activity1ActivityConfig = new Config(new EntityConfigId('activity', $activity1Class)); $activity1ActivityConfig->set('action_button_widget', 'button_widget1'); $activity1ActivityConfig->set('action_link_widget', 'link_widget1'); $activity2ExtendConfig = new Config(new EntityConfigId('extend', $activity2Class)); $activity2ExtendConfig->set('relation', [$activity2RelationKey => []]); $activity2EntityConfig = new Config(new EntityConfigId('entity', $activity2Class)); $activity2EntityConfig->set('plural_label', 'lbl.activity2'); $activity2ActivityConfig = new Config(new EntityConfigId('activity', $activity2Class)); $activity2ActivityConfig->set('action_button_widget', 'button_widget2'); $activity2ActivityConfig->set('action_link_widget', 'link_widget2'); $activity2ActivityConfig->set('priority', 100); $this->extendConfigProvider->expects($this->any())->method('getConfig')->will($this->returnValueMap([[$activity1Class, null, $activity1ExtendConfig], [$activity2Class, null, $activity2ExtendConfig]])); $this->entityConfigProvider->expects($this->any())->method('getConfig')->will($this->returnValueMap([[$activity1Class, null, $activity1EntityConfig], [$activity2Class, null, $activity2EntityConfig]])); $this->activityConfigProvider->expects($this->any())->method('getConfig')->will($this->returnValueMap([[$targetEntityClass, null, $targetEntityActivityConfig], [$activity1Class, null, $activity1ActivityConfig], [$activity2Class, null, $activity2ActivityConfig]])); $this->assertEquals([['className' => 'Test\\Activity1', 'associationName' => 'entity_1f801d4a', 'button_widget' => 'button_widget1', 'link_widget' => 'link_widget1'], ['className' => 'Test\\Activity2', 'associationName' => 'entity_1f801d4a', 'button_widget' => 'button_widget2', 'link_widget' => 'link_widget2', 'priority' => 100]], $this->manager->getActivityActions($targetEntityClass)); }
/** * @param FieldConfigId $fieldConfigId * * @return string */ protected function getRelationKey(FieldConfigId $fieldConfigId) { return ExtendHelper::buildRelationKey($fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), 'manyToOne', 'Oro\\Bundle\\AttachmentBundle\\Entity\\File'); }
/** * @param \PHPUnit_Framework_MockObject_MockObject $entityConfigProvider * @param \PHPUnit_Framework_MockObject_MockObject $extendConfigProvider * @param \PHPUnit_Framework_MockObject_MockObject $formConfigProvider * @param \PHPUnit_Framework_MockObject_MockObject $viewConfigProvider * @param \PHPUnit_Framework_MockObject_MockObject $importExportConfigProvider * @param string $entityClass * @param string $fieldName * @param string $label * @param string $description * @param string $targetEntity * @param string $targetField * @param int $iteration * * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ protected function addFieldAssertions(\PHPUnit_Framework_MockObject_MockObject $entityConfigProvider, \PHPUnit_Framework_MockObject_MockObject $extendConfigProvider, \PHPUnit_Framework_MockObject_MockObject $formConfigProvider, \PHPUnit_Framework_MockObject_MockObject $viewConfigProvider, \PHPUnit_Framework_MockObject_MockObject $importExportConfigProvider, $entityClass, $fieldName, $label, $description, $targetEntity, $targetField, $iteration) { $this->configManager->expects($this->at($iteration * 7 + 1))->method('hasConfig')->with($entityClass, $fieldName)->will($this->returnValue(false)); $this->configManager->expects($this->at($iteration * 7 + 2))->method('createConfigFieldModel')->with($entityClass, $fieldName, 'manyToOne'); $entityFieldConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $entityFieldConfig->expects($this->at(0))->method('set')->with('label', $label); $entityFieldConfig->expects($this->at(1))->method('set')->with('description', $description); $entityConfigProvider->expects($this->at($iteration))->method('getConfig')->with($entityClass, $fieldName)->will($this->returnValue($entityFieldConfig)); $extendFieldConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $extendFieldConfig->expects($this->at(0))->method('set')->with('owner', ExtendScope::OWNER_CUSTOM); $extendFieldConfig->expects($this->at(1))->method('set')->with('state', ExtendScope::STATE_NEW); $extendFieldConfig->expects($this->at(2))->method('set')->with('is_extend', true); $extendFieldConfig->expects($this->at(3))->method('set')->with('target_entity', $targetEntity); $extendFieldConfig->expects($this->at(4))->method('set')->with('target_field', $targetField); $extendFieldConfig->expects($this->at(5))->method('set')->with('relation_key', ExtendHelper::buildRelationKey($entityClass, $targetField, 'manyToOne', $targetEntity)); $extendConfigProvider->expects($this->at($iteration + 1))->method('getConfig')->with($entityClass, $fieldName)->will($this->returnValue($extendFieldConfig)); $formFieldConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $formFieldConfig->expects($this->at(0))->method('set')->with('is_enabled', false); $formConfigProvider->expects($this->at($iteration))->method('getConfig')->with($entityClass, $fieldName)->will($this->returnValue($formFieldConfig)); $viewFieldConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $viewFieldConfig->expects($this->at(0))->method('set')->with('is_displayable', false); $viewConfigProvider->expects($this->at($iteration))->method('getConfig')->with($entityClass, $fieldName)->will($this->returnValue($viewFieldConfig)); $importExportFieldConfig = $this->getMock('Oro\\Bundle\\EntityConfigBundle\\Config\\ConfigInterface'); $importExportFieldConfig->expects($this->at(0))->method('set')->with('excluded', true); $importExportConfigProvider->expects($this->at($iteration))->method('getConfig')->with($entityClass, $fieldName)->will($this->returnValue($importExportFieldConfig)); }
/** * Adds the inverse side of a many-to-one relation * * @param Schema $schema * @param Table|string $table A Table object or table name * @param string $associationName The name of a relation field * @param Table|string $targetTable A Table object or table name * @param string $targetAssociationName The name of a relation field on the inverse side * @param string[] $targetTitleColumnNames Column names are used to show a title of related entity * @param string[] $targetDetailedColumnNames Column names are used to show detailed info about related entity * @param string[] $targetGridColumnNames Column names are used to show related entity in a grid * @param array $options Entity config values * format is [CONFIG_SCOPE => [CONFIG_KEY => CONFIG_VALUE]] */ public function addManyToOneInverseRelation(Schema $schema, $table, $associationName, $targetTable, $targetAssociationName, array $targetTitleColumnNames, array $targetDetailedColumnNames, array $targetGridColumnNames, array $options = []) { $this->ensureExtendFieldSet($options); $selfTableName = $this->getTableName($table); $selfClassName = $this->getEntityClassByTableName($selfTableName); $targetTableName = $this->getTableName($targetTable); $targetTable = $this->getTable($targetTable, $schema); $targetClassName = $this->getEntityClassByTableName($targetTableName); $this->checkColumnsExist($targetTable, $targetTitleColumnNames); $this->checkColumnsExist($targetTable, $targetDetailedColumnNames); $this->checkColumnsExist($targetTable, $targetGridColumnNames); $relationKey = ExtendHelper::buildRelationKey($selfClassName, $associationName, RelationType::MANY_TO_ONE, $targetClassName); $targetFieldId = new FieldConfigId('extend', $targetClassName, $targetAssociationName, RelationType::ONE_TO_MANY); $selfTableOptions['extend']['relation.' . $relationKey . '.target_field_id'] = $targetFieldId; $this->extendOptionsManager->setTableOptions($selfTableName, $selfTableOptions); $targetTableOptions['extend']['relation.' . $relationKey . '.field_id'] = $targetFieldId; $this->extendOptionsManager->setTableOptions($targetTableName, $targetTableOptions); $options[ExtendOptionsManager::TARGET_OPTION] = ['table_name' => $selfTableName, 'relation_key' => $relationKey, 'columns' => ['title' => $targetTitleColumnNames, 'detailed' => $targetDetailedColumnNames, 'grid' => $targetGridColumnNames]]; $options[ExtendOptionsManager::TYPE_OPTION] = RelationType::ONE_TO_MANY; $this->extendOptionsManager->setColumnOptions($targetTableName, $targetAssociationName, $options); }
/** * @param string $tableName * @param string $columnName * @param array $options * * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function addColumnOptions($tableName, $columnName, $options) { $entityClassName = $this->getEntityClassName($tableName, null, false); if (!$entityClassName) { return; } $newColumnName = $this->getAndRemoveOption($options, ExtendOptionsManager::NEW_NAME_OPTION); if ($newColumnName) { $this->result[ExtendConfigProcessor::RENAME_CONFIGS][$entityClassName][$columnName] = $newColumnName; if (empty($options)) { return; } } $fieldName = $this->getAndRemoveOption($options, ExtendOptionsManager::FIELD_NAME_OPTION); if (!$fieldName) { $fieldName = $this->getFieldName($tableName, $columnName); } $columnType = $this->getAndRemoveOption($options, ExtendOptionsManager::TYPE_OPTION); $columnMode = $this->getAndRemoveOption($options, ExtendOptionsManager::MODE_OPTION); $columnUnderlyingType = $this->fieldTypeHelper->getUnderlyingType($columnType); if (in_array($columnUnderlyingType, RelationType::$anyToAnyRelations, true)) { if (!isset($options['extend'])) { $options['extend'] = []; } $target = $this->getAndRemoveOption($options, ExtendOptionsManager::TARGET_OPTION); foreach ($target as $optionName => $optionValue) { switch ($optionName) { case 'table_name': $options['extend']['target_entity'] = $this->getEntityClassName($optionValue); break; case 'column': $options['extend']['target_field'] = $this->getFieldName($target['table_name'], $optionValue); break; case 'columns': foreach ($optionValue as $group => $columns) { $values = []; foreach ($columns as $column) { $values[] = $this->getFieldName($target['table_name'], $column); } $options['extend']['target_' . $group] = $values; } break; default: $options['extend'][$optionName] = $optionValue; break; } } if (!isset($options['extend']['relation_key'])) { $options['extend']['relation_key'] = ExtendHelper::buildRelationKey($entityClassName, $fieldName, $columnUnderlyingType, $options['extend']['target_entity']); } } $this->result[$entityClassName]['fields'][$fieldName] = []; if (!empty($options)) { $this->result[$entityClassName]['fields'][$fieldName]['configs'] = $options; } if ($columnType) { $this->result[$entityClassName]['fields'][$fieldName]['type'] = $columnType; } if ($columnMode) { $this->result[$entityClassName]['fields'][$fieldName]['mode'] = $columnMode; } }
public function testBuildRelationKey() { $this->assertEquals('manyToOne|Test\\Entity|Test\\TargetEntity|testField', ExtendHelper::buildRelationKey('Test\\Entity', 'testField', 'manyToOne', 'Test\\TargetEntity')); }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testBuildManyToManyTargetSide() { $entityClass = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\TestClass'; $fieldName = 'srcField'; $fieldType = RelationType::MANY_TO_MANY; $fieldId = new FieldConfigId('extend', $entityClass, $fieldName, $fieldType); $targetEntityClass = 'Oro\\Bundle\\EntityExtendBundle\\Tests\\Unit\\Fixtures\\TestClass2'; $targetFieldName = 'targetField'; $targetFieldType = RelationType::MANY_TO_MANY; $targetFieldId = new FieldConfigId('extend', $targetEntityClass, $targetFieldName, $targetFieldType); $metadataBuilder = new ClassMetadataBuilder(new ClassMetadataInfo($entityClass)); $relationKey = ExtendHelper::buildRelationKey($entityClass, $fieldName, RelationType::MANY_TO_ONE, $targetEntityClass); $extendConfig = $this->getEntityConfig($entityClass, ['relation' => [$relationKey => ['field_id' => $fieldId, 'owner' => false, 'target_entity' => $targetEntityClass, 'target_field_id' => $targetFieldId]], 'schema' => ['relation' => [$fieldName => []]]]); $this->builder->build($metadataBuilder, $extendConfig); $result = $metadataBuilder->getClassMetadata()->getAssociationMapping($fieldName); $this->assertEquals(['sourceEntity' => $entityClass, 'targetEntity' => $targetEntityClass, 'fieldName' => $fieldName, 'type' => ClassMetadataInfo::MANY_TO_MANY, 'isOwningSide' => false, 'mappedBy' => $targetFieldName, 'inversedBy' => null, 'cascade' => [], 'joinTable' => [], 'fetch' => ClassMetadataInfo::FETCH_LAZY, 'isCascadeRemove' => false, 'isCascadePersist' => false, 'isCascadeRefresh' => false, 'isCascadeMerge' => false, 'isCascadeDetach' => false, 'orphanRemoval' => false], $result); $defaultRelationFieldName = ExtendConfigDumper::DEFAULT_PREFIX . $fieldName; $this->assertFalse($metadataBuilder->getClassMetadata()->hasAssociation($defaultRelationFieldName)); }
/** * @param ConfigInterface $fieldConfig */ protected function setRelationKeyToFieldConfig(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $relationKey = ExtendHelper::buildRelationKey($fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType()), $fieldConfig->get('target_entity')); $fieldConfig->set('relation_key', $relationKey); $this->configManager->persist($fieldConfig); }