From f1aba419a863974beda454d8c21107f673eae2f4 Mon Sep 17 00:00:00 2001 From: Vova Soroka Date: Wed, 23 Sep 2015 19:56:52 +0300 Subject: [PATCH 1/2] BAP-9064: Possible to created extended fields with identical names Conflicts: src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php Conflicts: src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php --- .../EnumEntityConfigDumperExtensionTest.php | 81 ++++++++++++++++++- .../EnumEntityConfigDumperExtension.php | 5 +- .../Tools/ExtendConfigDumper.php | 43 ++++++---- .../ExtendEntityGeneratorExtension.php | 42 +++++----- 4 files changed, 132 insertions(+), 39 deletions(-) diff --git a/src/Oro/Bundle/EntityExtendBundle/Tests/Unit/Tools/DumperExtensions/EnumEntityConfigDumperExtensionTest.php b/src/Oro/Bundle/EntityExtendBundle/Tests/Unit/Tools/DumperExtensions/EnumEntityConfigDumperExtensionTest.php index a0d8eab1cba..39e02ce76e9 100644 --- a/src/Oro/Bundle/EntityExtendBundle/Tests/Unit/Tools/DumperExtensions/EnumEntityConfigDumperExtensionTest.php +++ b/src/Oro/Bundle/EntityExtendBundle/Tests/Unit/Tools/DumperExtensions/EnumEntityConfigDumperExtensionTest.php @@ -560,8 +560,7 @@ public function testPostUpdateForMultiEnumFields() ] ], 'property' => [ - ExtendHelper::getMultiEnumSnapshotFieldName('field1') => - ExtendHelper::getMultiEnumSnapshotFieldName('field1') + ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [] ] ], $entityConfig1->get('schema') @@ -636,11 +635,85 @@ public function testPostUpdateForMultiEnumFieldsInCustomEntity() ] ], 'property' => [ - ExtendHelper::getMultiEnumSnapshotFieldName('field1') => - ExtendHelper::getMultiEnumSnapshotFieldName('field1') + ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [] ] ], $entityConfig1->get('schema') ); } + + public function testPostUpdateForDeletedMultiEnumField() + { + $entityConfig = new Config(new EntityConfigId('extend', 'Extend\EnumValue1')); + $entityConfig->set('owner', ExtendScope::OWNER_CUSTOM); + $entityConfig->set('is_extend', true); + $entityConfig->set( + 'schema', + [ + 'doctrine' => [ + 'Extend\EnumValue1' => [ + 'fields' => [ + ExtendHelper::getMultiEnumSnapshotFieldName('field2') => [ + 'column' => 'field2' + ] + ] + ] + ] + ] + ); + + $fieldConfig = new Config(new FieldConfigId('extend', 'Extend\EnumValue1', 'field1', 'multiEnum')); + $fieldConfig->set('is_deleted', true); + + $entityConfigs = [$entityConfig]; + $fieldConfigs = [$fieldConfig]; + + $extendConfigProvider = $this->getMockBuilder('Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider') + ->disableOriginalConstructor() + ->getMock(); + $this->configManager->expects($this->once()) + ->method('getProvider') + ->with('extend') + ->will($this->returnValue($extendConfigProvider)); + $extendConfigProvider->expects($this->at(0)) + ->method('getConfigs') + ->with(null, true) + ->will($this->returnValue($entityConfigs)); + $extendConfigProvider->expects($this->at(1)) + ->method('getConfigs') + ->with($entityConfig->getId()->getClassName()) + ->will($this->returnValue($fieldConfigs)); + + $this->configManager->expects($this->once()) + ->method('persist') + ->with($this->identicalTo($entityConfig)); + + $this->extension->postUpdate(); + + $this->assertEquals( + [ + 'doctrine' => [ + 'Extend\EnumValue1' => [ + 'fields' => [ + ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [ + 'column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName('field1'), + 'type' => 'string', + 'nullable' => true, + 'length' => ExtendHelper::MAX_ENUM_SNAPSHOT_LENGTH, + ], + ExtendHelper::getMultiEnumSnapshotFieldName('field2') => [ + 'column' => 'field2' + ] + ] + ] + ], + 'property' => [ + ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [ + 'private' => true + ] + ] + ], + $entityConfig->get('schema') + ); + } } diff --git a/src/Oro/Bundle/EntityExtendBundle/Tools/DumperExtensions/EnumEntityConfigDumperExtension.php b/src/Oro/Bundle/EntityExtendBundle/Tools/DumperExtensions/EnumEntityConfigDumperExtension.php index 77868cd5f00..d6f76a4d469 100644 --- a/src/Oro/Bundle/EntityExtendBundle/Tools/DumperExtensions/EnumEntityConfigDumperExtension.php +++ b/src/Oro/Bundle/EntityExtendBundle/Tools/DumperExtensions/EnumEntityConfigDumperExtension.php @@ -190,7 +190,10 @@ public function postUpdate() continue; } - $schema['property'][$snapshotFieldName] = $snapshotFieldName; + $schema['property'][$snapshotFieldName] = []; + if ($fieldConfig->is('is_deleted')) { + $schema['property'][$snapshotFieldName]['private'] = true; + } $schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName] = [ 'column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName($fieldName), diff --git a/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php b/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php index 3074b77a9fb..b749166b4b0 100644 --- a/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php +++ b/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php @@ -204,22 +204,37 @@ protected function checkFields( array &$properties, array &$doctrine ) { + if ($fieldConfig->is('state', ExtendScope::STATE_DELETE)) { + $fieldConfig->set('is_deleted', true); + } else { + $fieldConfig->set('state', ExtendScope::STATE_ACTIVE); + } if ($fieldConfig->is('is_extend')) { /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $fieldConfig->getId(); $fieldName = $fieldConfigId->getFieldName(); $fieldType = $fieldConfigId->getFieldType(); + $isDeleted = $fieldConfig->is('is_deleted'); $underlyingFieldType = $this->fieldTypeHelper->getUnderlyingType($fieldType); if (in_array($underlyingFieldType, array_merge(RelationType::$anyToAnyRelations, ['optionSet']))) { $relationProperties[$fieldName] = $fieldName; + if ($isDeleted) { + $relationProperties[$fieldName]['private'] = true; + } if ($underlyingFieldType !== RelationType::MANY_TO_ONE && !$fieldConfig->is('without_default')) { $defaultName = self::DEFAULT_PREFIX . $fieldName; - $defaultProperties[$defaultName] = $defaultName; + $defaultProperties[$defaultName] = []; + if ($isDeleted) { + $defaultProperties[$defaultName]['private'] = true; + } } } else { - $properties[$fieldName] = $fieldName; + $properties[$fieldName] = []; + if ($isDeleted) { + $properties[$fieldName]['private'] = true; + } $doctrine[$entityName]['fields'][$fieldName] = [ 'column' => $fieldName, @@ -231,12 +246,6 @@ protected function checkFields( ]; } } - - if ($fieldConfig->is('state', ExtendScope::STATE_DELETE)) { - $fieldConfig->set('is_deleted', true); - } else { - $fieldConfig->set('state', ExtendScope::STATE_ACTIVE); - } } /** @@ -308,13 +317,17 @@ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $s $relation['assign'] = true; if ($relation['field_id']->getFieldType() !== RelationType::MANY_TO_ONE) { $fieldName = $relation['field_id']->getFieldName(); - - $addRemoveMethods[$fieldName]['self'] = $fieldName; - if ($relation['target_field_id']) { - $addRemoveMethods[$fieldName]['target'] = - $relation['target_field_id']->getFieldName(); - $addRemoveMethods[$fieldName]['is_target_addremove'] = - $relation['field_id']->getFieldType() === RelationType::MANY_TO_MANY; + $fieldConfig = $extendProvider->getConfig($relation['field_id']->getClassName(), $fieldName); + $isDeleted = $fieldConfig->is('is_deleted'); + + if (!$isDeleted) { + $addRemoveMethods[$fieldName]['self'] = $fieldName; + if ($relation['target_field_id']) { + $addRemoveMethods[$fieldName]['target'] = + $relation['target_field_id']->getFieldName(); + $addRemoveMethods[$fieldName]['is_target_addremove'] = + $relation['field_id']->getFieldType() === RelationType::MANY_TO_MANY; + } } } diff --git a/src/Oro/Bundle/EntityExtendBundle/Tools/GeneratorExtensions/ExtendEntityGeneratorExtension.php b/src/Oro/Bundle/EntityExtendBundle/Tools/GeneratorExtensions/ExtendEntityGeneratorExtension.php index 3c664263e91..ba22df543f6 100644 --- a/src/Oro/Bundle/EntityExtendBundle/Tools/GeneratorExtensions/ExtendEntityGeneratorExtension.php +++ b/src/Oro/Bundle/EntityExtendBundle/Tools/GeneratorExtensions/ExtendEntityGeneratorExtension.php @@ -89,15 +89,15 @@ protected function generateToStringMethod(array $schema, PhpClass $class) { $toString = []; foreach ($schema['property'] as $fieldName => $config) { - if ($schema['doctrine'][$schema['entity']]['fields'][$fieldName]['type'] == 'string') { + $isPrivate = is_array($config) && isset($config['private']) && $config['private']; + if (!$isPrivate && $schema['doctrine'][$schema['entity']]['fields'][$fieldName]['type'] === 'string') { $toString[] = '$this->' . $this->generateGetMethodName($fieldName) . '()'; } } - $toStringBody = 'return (string) $this->getId();'; - if (count($toString) > 0) { - $toStringBody = 'return (string)' . implode(' . ', $toString) . ';'; - } + $toStringBody = empty($toString) + ? 'return (string) $this->getId();' + : 'return (string)' . implode(' . ', $toString) . ';'; $class->setMethod($this->generateClassMethod('__toString', $toStringBody)); } @@ -109,21 +109,25 @@ protected function generateToStringMethod(array $schema, PhpClass $class) protected function generateProperties($propertyType, array $schema, PhpClass $class) { foreach ($schema[$propertyType] as $fieldName => $config) { - $class - ->setProperty(PhpProperty::create($fieldName)->setVisibility('protected')) - ->setMethod( - $this->generateClassMethod( - $this->generateGetMethodName($fieldName), - 'return $this->' . $fieldName . ';' + $class->setProperty(PhpProperty::create($fieldName)->setVisibility('protected')); + + $isPrivate = is_array($config) && isset($config['private']) && $config['private']; + if (!$isPrivate) { + $class + ->setMethod( + $this->generateClassMethod( + $this->generateGetMethodName($fieldName), + 'return $this->' . $fieldName . ';' + ) ) - ) - ->setMethod( - $this->generateClassMethod( - $this->generateSetMethodName($fieldName), - $this->getSetterBody($fieldName, $schema), - ['value'] - ) - ); + ->setMethod( + $this->generateClassMethod( + $this->generateSetMethodName($fieldName), + $this->getSetterBody($fieldName, $schema), + ['value'] + ) + ); + } } } From 5d2fbbb13d1efc891bdcee975503a1dbeec11762 Mon Sep 17 00:00:00 2001 From: Vova Soroka Date: Wed, 23 Sep 2015 21:52:09 +0300 Subject: [PATCH 2/2] BAP-9064: Possible to created extended fields with identical names Conflicts: src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php --- .../Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php b/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php index b749166b4b0..70fa8acd8cf 100644 --- a/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php +++ b/src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php @@ -317,8 +317,9 @@ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $s $relation['assign'] = true; if ($relation['field_id']->getFieldType() !== RelationType::MANY_TO_ONE) { $fieldName = $relation['field_id']->getFieldName(); - $fieldConfig = $extendProvider->getConfig($relation['field_id']->getClassName(), $fieldName); - $isDeleted = $fieldConfig->is('is_deleted'); + $isDeleted = $extendProvider->hasConfig($relation['field_id']->getClassName(), $fieldName) + ? $extendProvider->getConfig($relation['field_id']->getClassName(), $fieldName)->is('is_deleted') + : false; if (!$isDeleted) { $addRemoveMethods[$fieldName]['self'] = $fieldName;