public function testGetConfig() { $this->assertEquals('Test\\Class', $this->fieldId->getClassName()); $this->assertEquals('testScope', $this->fieldId->getScope()); $this->assertEquals('testField', $this->fieldId->getFieldName()); $this->assertEquals('string', $this->fieldId->getFieldType()); $this->assertEquals('field_testScope_Test-Class_testField', $this->fieldId->toString()); $this->fieldId->setFieldType('integer'); $this->assertEquals('integer', $this->fieldId->getFieldType()); }
/** * @param FieldConfigId $selfFieldId * @param string $targetEntity * @return string */ public static function generateManyToManyJoinTableName(FieldConfigId $selfFieldId, $targetEntity) { $selfClassArray = explode('\\', $selfFieldId->getClassName()); $selfClassName = array_pop($selfClassArray); $targetClassArray = explode('\\', $targetEntity); $targetClassName = array_pop($targetClassArray); return strtolower('oro_extend_' . $selfClassName . '_' . $targetClassName . '_' . $selfFieldId->getFieldName()); }
/** * @param ConfigInterface $extendConfig * @param FieldConfigId $fieldConfigId * * @return array */ protected function getOptions(ConfigInterface $extendConfig, FieldConfigId $fieldConfigId) { $className = $fieldConfigId->getClassName(); $fieldName = $fieldConfigId->getFieldName(); $options = []; switch ($fieldConfigId->getFieldType()) { case 'boolean': $options['empty_value'] = false; $options['choices'] = ['No', 'Yes']; break; case 'optionSet': $options['entityClassName'] = $className; $options['entityFieldName'] = $fieldName; break; case 'enum': $options['enum_code'] = $this->enumConfigProvider->getConfig($className, $fieldName)->get('enum_code'); break; case 'multiEnum': $options['expanded'] = true; $options['enum_code'] = $this->enumConfigProvider->getConfig($className, $fieldName)->get('enum_code'); break; case RelationType::MANY_TO_ONE: $options['entity_class'] = $extendConfig->get('target_entity'); $options['configs'] = ['placeholder' => 'oro.form.choose_value', 'component' => 'relation', 'target_entity' => str_replace('\\', '_', $extendConfig->get('target_entity')), 'target_field' => $extendConfig->get('target_field'), 'properties' => [$extendConfig->get('target_field')]]; break; case RelationType::ONE_TO_MANY: case RelationType::MANY_TO_MANY: $classArray = explode('\\', $extendConfig->get('target_entity')); $blockName = array_pop($classArray); $options['block'] = $blockName; $options['block_config'] = [$blockName => ['title' => null, 'subblocks' => [['useSpan' => false]]]]; $options['class'] = $extendConfig->get('target_entity'); $options['selector_window_title'] = 'Select ' . $blockName; $options['initial_elements'] = null; if (!$extendConfig->is('without_default')) { $options['default_element'] = ExtendConfigDumper::DEFAULT_PREFIX . $fieldName; } break; } return $options; }
public function testNewEnumNameValidators() { $configId = new FieldConfigId('enum', 'Test\\Entity', 'testField', 'enum'); $this->typeHelper->expects($this->any())->method('hasEnumCode')->with($configId->getClassName(), $configId->getFieldName())->will($this->returnValue(false)); $resolver = $this->getOptionsResolver(); $this->type->setDefaultOptions($resolver); $resolvedOptions = $resolver->resolve(['config_id' => $configId]); $this->assertCount(5, $resolvedOptions['constraints']); $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\NotBlank', $resolvedOptions['constraints'][0]); $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Length', $resolvedOptions['constraints'][1]); $this->assertEquals($this->nameGenerator->getMaxEnumCodeSize(), $resolvedOptions['constraints'][1]->max); $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Regex', $resolvedOptions['constraints'][2]); $this->assertEquals('/^[\\w- ]*$/', $resolvedOptions['constraints'][2]->pattern); $this->assertEquals(EnumNameType::INVALID_NAME_MESSAGE, $resolvedOptions['constraints'][2]->message); $this->assertInstanceOf('Symfony\\Component\\Validator\\Constraints\\Callback', $resolvedOptions['constraints'][3]); $context = $this->getMockBuilder('Symfony\\Component\\Validator\\ExecutionContext')->disableOriginalConstructor()->getMock(); $context->expects($this->once())->method('addViolation')->with(EnumNameType::INVALID_NAME_MESSAGE); call_user_func($resolvedOptions['constraints'][3]->methods[0], '!@#$', $context); $this->assertInstanceOf('Oro\\Bundle\\EntityExtendBundle\\Validator\\Constraints\\UniqueEnumName', $resolvedOptions['constraints'][4]); $this->assertEquals($configId->getClassName(), $resolvedOptions['constraints'][4]->entityClassName); $this->assertEquals($configId->getFieldName(), $resolvedOptions['constraints'][4]->fieldName); }
public function testClearFieldCache() { $configId = new FieldConfigId('entity', self::ENTITY_CLASS, 'field'); $this->configCache->expects($this->once())->method('deleteFieldConfig')->with($configId->getClassName(), $configId->getFieldName()); $this->configManager->clearCache($configId); }
/** * @param ClassMetadataBuilder $metadataBuilder * @param FieldConfigId $fieldId * @param string $targetEntity * @param FieldConfigId|null $targetFieldId * @param string[] $cascade */ protected function buildManyToManyOwningSideRelation(ClassMetadataBuilder $metadataBuilder, FieldConfigId $fieldId, $targetEntity, FieldConfigId $targetFieldId = null, array $cascade = []) { $builder = $metadataBuilder->createManyToMany($fieldId->getFieldName(), $targetEntity); if ($targetFieldId) { $builder->inversedBy($targetFieldId->getFieldName()); } $builder->setJoinTable($this->nameGenerator->generateManyToManyJoinTableName($fieldId->getClassName(), $fieldId->getFieldName(), $targetEntity)); foreach ($cascade as $cascadeType) { $builder->{'cascade' . ucfirst($cascadeType)}(); } $builder->build(); $extendFieldConfig = $this->extendConfigProvider->getConfigById($fieldId); if (!$extendFieldConfig->is('without_default')) { $this->buildDefaultRelation($metadataBuilder, $fieldId, $targetEntity); } }
public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('attr' => array('class' => 'extend-rel-target-name'), 'label' => 'oro.entity_extend.form.target_entity', 'empty_value' => $this->targetEntity ? null : '', 'read_only' => (bool) $this->targetEntity, 'choices' => $this->getEntityChoiceList($this->configId->getClassName(), $this->configId->getFieldType()), 'choice_attr' => function ($choice) { return $this->getChoiceAttributes($choice); }, 'configs' => array('allowClear' => true, 'placeholder' => 'oro.entity.form.choose_entity', 'result_template_twig' => 'OroEntityBundle:Choice:entity/result.html.twig', 'selection_template_twig' => 'OroEntityBundle:Choice:entity/selection.html.twig'))); }
/** * @param ConfigInterface $fieldConfig * @param string $relationKey */ protected function createTargetRelation(ConfigInterface $fieldConfig, $relationKey) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $targetEntityClass = $fieldConfig->get('target_entity'); $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName()); $selfRelations = $selfConfig->get('relation'); $selfRelationConfig =& $selfRelations[$relationKey]; $selfRelationConfig['field_id'] = $selfFieldId; $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass); $targetRelations = $targetConfig->get('relation'); $targetRelationConfig =& $targetRelations[$relationKey]; $targetRelationConfig['target_field_id'] = $selfFieldId; $selfConfig->set('relation', $selfRelations); $targetConfig->set('relation', $targetRelations); $this->extendConfigProvider->persist($targetConfig); }
/** * @param FieldConfigId $fieldConfigId * * @return string */ protected function getRelationKey(FieldConfigId $fieldConfigId) { return ExtendHelper::buildRelationKey($fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), 'manyToOne', 'Oro\\Bundle\\AttachmentBundle\\Entity\\File'); }
/** * @param string $scope * @param FieldConfigId $fieldId * * @return ConfigInterface */ protected function getFieldConfig($scope, FieldConfigId $fieldId) { return $this->configManager->getProvider($scope)->getConfig($fieldId->getClassName(), $fieldId->getFieldName()); }
/** * @param FieldConfigId $fieldId * * @return ConfigInterface */ protected function getFieldConfig(FieldConfigId $fieldId) { return $this->configManager->getFieldConfig('extend', $fieldId->getClassName(), $fieldId->getFieldName()); }
public function testDeleteFieldConfig() { $configId = new FieldConfigId(self::SCOPE, self::ENTITY_CLASS, self::FIELD_NAME, self::FIELD_TYPE); $cacheKey = self::ENTITY_CLASS . '.' . self::FIELD_NAME; $this->cache->expects($this->at(0))->method('delete')->with(ConfigCache::FIELD_NAMES_KEY . self::ENTITY_CLASS)->willReturn(true); $this->cache->expects($this->at(1))->method('delete')->with($cacheKey)->willReturn(true); $this->assertTrue($this->configCache->deleteFieldConfig($configId->getClassName(), $configId->getFieldName())); }
/** * @param ConfigInterface $fieldConfig */ protected function createSelfRelation(ConfigInterface $fieldConfig) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $targetEntityClass = $fieldConfig->get('target_entity'); $selfFieldId = $selfFieldId = new FieldConfigId('extend', $fieldConfigId->getClassName(), $fieldConfigId->getFieldName(), $this->fieldTypeHelper->getUnderlyingType($fieldConfigId->getFieldType())); $targetFieldId = false; $owner = true; $targetOwner = false; if (in_array($selfFieldId->getFieldType(), RelationType::$toManyRelations, true)) { $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())); } $relationKey = ExtendHelper::buildRelationKey($selfFieldId->getClassName(), $selfFieldId->getFieldName(), $selfFieldId->getFieldType(), $targetEntityClass); $selfConfig = $this->extendConfigProvider->getConfig($selfFieldId->getClassName()); $selfRelationConfig = ['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', false, []); $selfRelations[$relationKey] = $selfRelationConfig; $selfConfig->set('relation', $selfRelations); $this->configManager->persist($selfConfig); $targetConfig = $this->extendConfigProvider->getConfig($targetEntityClass); $targetRelationConfig = ['field_id' => $targetFieldId, 'owner' => $targetOwner, 'target_entity' => $selfFieldId->getClassName(), 'target_field_id' => $selfFieldId]; $targetRelations = $targetConfig->get('relation', false, []); $targetRelations[$relationKey] = $targetRelationConfig; $targetConfig->set('relation', $targetRelations); $this->configManager->persist($targetConfig); $fieldConfig->set('relation_key', $relationKey); $this->configManager->persist($fieldConfig); }
public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array('attr' => array('class' => 'extend-rel-target-name'), 'label' => 'Target entity', 'empty_value' => $this->targetEntity ? false : 'Please choice target entity...', 'read_only' => (bool) $this->targetEntity, 'choices' => $this->getEntityChoiceList($this->configId->getClassName(), $this->configId->getFieldType()))); }
/** * @param object $entity * @param FieldConfigId $fieldConfig * @return OptionSetRelation[] */ protected function getValueForOptionSet($entity, FieldConfigId $fieldConfig) { /** @var $optionSetRepository OptionSetRelationRepository */ $optionSetRepository = $this->configManager->getEntityManager()->getRepository(OptionSetRelation::ENTITY_NAME); $model = $this->configManager->getConfigFieldModel($fieldConfig->getClassName(), $fieldConfig->getFieldName()); $value = $optionSetRepository->findByFieldId($model->getId(), $entity->getId()); array_walk($value, function (OptionSetRelation &$item) { $item = array('title' => $item->getOption()->getLabel()); }); $value['values'] = $value; return $value; }
/** * Changes a type of a field * * @param string $className * @param string $fieldName * @param string $newFieldName * @return bool TRUE if the name was changed; otherwise, FALSE */ public function changeFieldName($className, $fieldName, $newFieldName) { $result = $this->modelManager->changeFieldName($className, $fieldName, $newFieldName); if ($result) { $this->eventDispatcher->dispatch(Events::RENAME_FIELD, new RenameFieldEvent($className, $fieldName, $newFieldName, $this)); foreach ($this->getProviders() as $provider) { /** @var FieldConfigId $newConfigId */ $newConfigId = $this->getId($provider->getScope(), $className, $newFieldName); $newConfigKey = $this->buildConfigKey($newConfigId); $configId = new FieldConfigId($newConfigId->getScope(), $newConfigId->getClassName(), $fieldName, $newConfigId->getFieldType()); $cachedConfig = $this->cache->getFieldConfig($configId->getScope(), $configId->getClassName(), $configId->getFieldName(), true); if ($cachedConfig) { $this->cache->saveConfig($this->changeConfigFieldName($cachedConfig, $newFieldName), true); $this->cache->deleteFieldConfig($configId->getClassName(), $configId->getFieldName(), true); } $configKey = $this->buildConfigKey($configId); if (isset($this->persistConfigs[$configKey])) { $this->persistConfigs[$newConfigKey] = $this->changeConfigFieldName($this->persistConfigs[$configKey], $newFieldName); unset($this->persistConfigs[$configKey]); } if (isset($this->originalConfigs[$configKey])) { $this->originalConfigs[$newConfigKey] = $this->changeConfigFieldName($this->originalConfigs[$configKey], $newFieldName); unset($this->originalConfigs[$configKey]); } if (isset($this->configChangeSets[$configKey])) { $this->configChangeSets[$newConfigKey] = $this->configChangeSets[$configKey]; unset($this->configChangeSets[$configKey]); } } } return $result; }