/** * Creates a table for a custom entity. * The custom entity is an entity which has no PHP class in any bundle. The definition of such entity is * created automatically in Symfony cache * * @param Schema $schema * @param string $entityName * @param array $options * * @return Table * * @throws \InvalidArgumentException */ public function createCustomEntityTable(Schema $schema, $entityName, array $options = []) { $className = ExtendHelper::ENTITY_NAMESPACE . $entityName; $tableName = $this->nameGenerator->generateCustomEntityTableName($className); $table = $schema->createTable($tableName); $this->entityMetadataHelper->registerEntityClass($tableName, $className); $options = new OroOptions($options); // set options $options->setAuxiliary(ExtendOptionsManager::ENTITY_CLASS_OPTION, $className); if ($options->has('extend', 'owner')) { if ($options->get('extend', 'owner') !== ExtendScope::OWNER_CUSTOM) { throw new \InvalidArgumentException(sprintf('The "extend.owner" option for a custom entity must be "%s".', ExtendScope::OWNER_CUSTOM)); } } else { $options->set('extend', 'owner', ExtendScope::OWNER_CUSTOM); } if ($options->has('extend', 'is_extend')) { if ($options->get('extend', 'is_extend') !== true) { throw new \InvalidArgumentException('The "extend.is_extend" option for a custom entity must be TRUE.'); } } else { $options->set('extend', 'is_extend', true); } $table->addOption(OroOptions::KEY, $options); // add a primary key $table->addColumn(self::AUTO_GENERATED_ID_COLUMN_NAME, 'integer', ['autoincrement' => true]); $table->setPrimaryKey([self::AUTO_GENERATED_ID_COLUMN_NAME]); return $table; }
/** * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @param ConfigInterface $extendConfig * @param array|null $aliases * @param array|null $skippedOrigins */ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $skippedOrigins = null) { $extendProvider = $this->em->getExtendConfigProvider(); $className = $extendConfig->getId()->getClassName(); $doctrine = []; $entityName = $className; if (ExtendHelper::isCustomEntity($className)) { $type = 'Custom'; $tableName = $extendConfig->get('table'); if (!$tableName) { $tableName = $this->nameGenerator->generateCustomEntityTableName($className); } $doctrine[$entityName] = ['type' => 'entity', 'table' => $tableName]; // add 'id' field only for Custom entity without inheritance if (!$extendConfig->has('inherit')) { $doctrine[$entityName]['fields'] = ['id' => ['type' => 'integer', 'id' => true, 'generator' => ['strategy' => 'AUTO']]]; } } else { $type = 'Extend'; $entityName = $extendConfig->get('extend_class'); $doctrine[$entityName] = ['type' => 'mappedSuperclass', 'fields' => []]; } $schema = $extendConfig->get('schema'); $properties = []; $relationProperties = $schema ? $schema['relation'] : []; $defaultProperties = []; $addRemoveMethods = []; $fieldConfigs = $extendProvider->filter($this->createOriginFilterCallback($skippedOrigins), $className, true); foreach ($fieldConfigs as $fieldConfig) { $this->checkFields($entityName, $fieldConfig, $relationProperties, $defaultProperties, $properties, $doctrine); $extendProvider->persist($fieldConfig); } $relations = $extendConfig->get('relation', false, []); foreach ($relations as &$relation) { if (!$relation['field_id']) { continue; } $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; } } $this->updateRelationValues($relation['target_entity'], $relation['field_id']); } $extendConfig->set('relation', $relations); $schema = ['class' => $className, 'entity' => $entityName, 'type' => $type, 'property' => $properties, 'relation' => $relationProperties, 'default' => $defaultProperties, 'addremove' => $addRemoveMethods, 'doctrine' => $doctrine]; if ($type == 'Extend') { $parentClassName = get_parent_class($className); if ($parentClassName == $entityName) { $parentClassName = $aliases[$entityName]; } $schema['parent'] = $parentClassName; $schema['inherit'] = get_parent_class($parentClassName); } elseif ($extendConfig->has('inherit')) { $schema['inherit'] = $extendConfig->get('inherit'); } $extendConfig->set('schema', $schema); $extendProvider->persist($extendConfig); }
/** * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * * @param ConfigInterface $extendConfig * @param ConfigProvider $configProvider * @param array|null $aliases * @param callable|null $filter function (ConfigInterface $config) : bool */ protected function checkSchema(ConfigInterface $extendConfig, ConfigProvider $configProvider, $aliases, $filter = null) { $className = $extendConfig->getId()->getClassName(); $doctrine = []; $entityName = $className; if (ExtendHelper::isCustomEntity($className)) { $type = 'Custom'; $tableName = $extendConfig->get('table'); if (!$tableName) { $tableName = $this->nameGenerator->generateCustomEntityTableName($className); } $doctrine[$entityName] = ['type' => 'entity', 'table' => $tableName]; // add 'id' field only for Custom entity without inheritance if (!$extendConfig->has('inherit')) { $doctrine[$entityName]['fields'] = ['id' => ['type' => 'integer', 'id' => true, 'generator' => ['strategy' => 'AUTO']]]; } } else { $type = 'Extend'; $entityName = $extendConfig->get('extend_class'); $doctrine[$entityName] = ['type' => 'mappedSuperclass', 'fields' => []]; } $schema = $extendConfig->get('schema', false, []); $properties = isset($schema['property']) && null !== $filter ? $schema['property'] : []; $relationProperties = isset($schema['relation']) && null !== $filter ? $schema['relation'] : []; $defaultProperties = isset($schema['default']) && null !== $filter ? $schema['default'] : []; $addRemoveMethods = isset($schema['addremove']) && null !== $filter ? $schema['addremove'] : []; $fieldConfigs = null === $filter ? $configProvider->getConfigs($className, true) : $configProvider->filter($filter, $className, true); foreach ($fieldConfigs as $fieldConfig) { $this->updateFieldState($fieldConfig); $this->checkFieldSchema($entityName, $fieldConfig, $relationProperties, $defaultProperties, $properties, $doctrine); } $relations = $extendConfig->get('relation', false, []); foreach ($relations as $relation) { /** @var FieldConfigId $fieldId */ $fieldId = $relation['field_id']; if (!$fieldId) { continue; } $fieldName = $fieldId->getFieldName(); $isDeleted = $configProvider->hasConfig($fieldId->getClassName(), $fieldName) ? $configProvider->getConfig($fieldId->getClassName(), $fieldName)->is('is_deleted') : false; if (!isset($relationProperties[$fieldName])) { $relationProperties[$fieldName] = []; if ($isDeleted) { $relationProperties[$fieldName]['private'] = true; } } if (!$isDeleted && $fieldId->getFieldType() !== RelationType::MANY_TO_ONE) { $addRemoveMethods[$fieldName]['self'] = $fieldName; /** @var FieldConfigId $targetFieldId */ $targetFieldId = $relation['target_field_id']; if ($targetFieldId) { $fieldType = $fieldId->getFieldType(); $addRemoveMethods[$fieldName]['target'] = $targetFieldId->getFieldName(); $addRemoveMethods[$fieldName]['is_target_addremove'] = $fieldType === RelationType::MANY_TO_MANY; } } } $schema = ['class' => $className, 'entity' => $entityName, 'type' => $type, 'property' => $properties, 'relation' => $relationProperties, 'default' => $defaultProperties, 'addremove' => $addRemoveMethods, 'doctrine' => $doctrine]; if ($type === 'Extend') { $parentClassName = get_parent_class($className); if ($parentClassName === $entityName) { $parentClassName = $aliases[$entityName]; } $schema['parent'] = $parentClassName; $schema['inherit'] = get_parent_class($parentClassName); } elseif ($extendConfig->has('inherit')) { $schema['inherit'] = $extendConfig->get('inherit'); } $extendConfig->set('schema', $schema); $this->configManager->persist($extendConfig); }