コード例 #1
0
 /**
  * @param LoggerInterface $logger
  * @param bool            $dryRun
  */
 protected function runActivityLists(LoggerInterface $logger, $dryRun = false)
 {
     // @todo: this workaround should be removed in BAP-9156
     $this->configManager->clear();
     $targetEntities = $this->provider->getTargetEntityClasses(false);
     $toSchema = clone $this->schema;
     $hasSchemaChanges = false;
     foreach ($targetEntities as $targetEntity) {
         $associationName = ExtendHelper::buildAssociationName($targetEntity, ActivityListEntityConfigDumperExtension::ASSOCIATION_KIND);
         $relationTableName = $this->nameGenerator->generateManyToManyJoinTableName(ActivityListEntityConfigDumperExtension::ENTITY_CLASS, $associationName, $targetEntity);
         if (!$toSchema->hasTable($relationTableName)) {
             $hasSchemaChanges = true;
             $this->activityListExtension->addActivityListAssociation($toSchema, $this->metadataHelper->getTableNameByEntityClass($targetEntity));
         }
     }
     if ($hasSchemaChanges) {
         $comparator = new Comparator();
         $platform = $this->connection->getDatabasePlatform();
         $schemaDiff = $comparator->compare($this->schema, $toSchema);
         $queries = $schemaDiff->toSql($platform);
         foreach ($queries as $query) {
             $this->logQuery($logger, $query);
             if (!$dryRun) {
                 $this->connection->executeQuery($query);
             }
         }
     }
 }
コード例 #2
0
 public function testGetFieldNameByColumnName()
 {
     $metadata = $this->getMockBuilder('Doctrine\\ORM\\Mapping\\ClassMetadata')->disableOriginalConstructor()->getMock();
     $metadata->expects($this->once())->method('getTableName')->will($this->returnValue('acme_test'));
     $metadata->expects($this->once())->method('getFieldName')->with('name_column')->will($this->returnValue('name_field'));
     $metadata->expects($this->once())->method('getName')->will($this->returnValue('Oro\\Bundle\\EntityBundle\\Tests\\Unit\\ORM\\Fixtures\\TestEntity'));
     $metadataFactory = $this->getMockBuilder('Doctrine\\ORM\\Mapping\\ClassMetadataFactory')->disableOriginalConstructor()->getMock();
     $metadataFactory->expects($this->once())->method('getAllMetadata')->will($this->returnValue([$metadata]));
     $em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $em->expects($this->once())->method('getMetadataFactory')->will($this->returnValue($metadataFactory));
     $em->expects($this->once())->method('getClassMetadata')->with('Oro\\Bundle\\EntityBundle\\Tests\\Unit\\ORM\\Fixtures\\TestEntity')->will($this->returnValue($metadata));
     $this->doctrine->expects($this->once())->method('getManagers')->will($this->returnValue(array('default' => $em)));
     $this->doctrine->expects($this->once())->method('getManager')->with($this->equalTo('default'))->will($this->returnValue($em));
     $this->doctrine->expects($this->once())->method('getManagerForClass')->with('Oro\\Bundle\\EntityBundle\\Tests\\Unit\\ORM\\Fixtures\\TestEntity')->will($this->returnValue($em));
     $this->assertEquals('name_field', $this->helper->getFieldNameByColumnName('acme_test', 'name_column'));
 }
コード例 #3
0
 /**
  * @param Schema   $schema
  * @param QueryBag $queries
  * @param string   $tableName
  * @param string   $columnName
  * @param array    $options
  */
 protected function processColumn(Schema $schema, QueryBag $queries, $tableName, $columnName, $options)
 {
     $className = $this->entityMetadataHelper->getEntityClassByTableName($tableName);
     $table = $schema->getTable($tableName);
     if (!isset($options[ExtendOptionsManager::NEW_NAME_OPTION])) {
         if (isset($options[ExtendOptionsManager::TYPE_OPTION])) {
             $columnType = $options[ExtendOptionsManager::TYPE_OPTION];
             if (!in_array($columnType, ['oneToMane', 'manyToMany', 'manyToOne', 'optionSet'])) {
                 $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName);
                 $enabled = !isset($options['datagrid']['is_visible']) || $options['datagrid']['is_visible'];
                 if ($enabled && !$table->hasIndex($indexName)) {
                     $table->addIndex([$columnName], $indexName);
                 } elseif (!$enabled && $table->hasIndex($indexName)) {
                     $table->dropIndex($indexName);
                 }
             }
         }
     } else {
         // in case of renaming column name we should rename existing index
         $newColumnName = $options[ExtendOptionsManager::NEW_NAME_OPTION];
         $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName);
         if ($table->hasIndex($indexName)) {
             $table->dropIndex($indexName);
             $newIndexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $newColumnName);
             $this->renameExtension->addIndex($schema, $queries, $tableName, [$newColumnName], $newIndexName);
         }
     }
 }
コード例 #4
0
 /**
  * @param LoggerInterface $logger
  * @param bool            $dryRun
  */
 protected function addActivityContactColumns(LoggerInterface $logger, $dryRun = false)
 {
     $hasSchemaChanges = false;
     $toSchema = clone $this->schema;
     $contactingActivityClasses = $this->activityContactProvider->getSupportedActivityClasses();
     $entities = $this->getConfigurableEntitiesData($logger);
     foreach ($entities as $entityClassName => $config) {
         if (isset($config['extend']['is_extend'], $config['activity']['activities']) && $config['extend']['is_extend'] == true && $config['activity']['activities'] && array_intersect($contactingActivityClasses, $config['activity']['activities'])) {
             if (isset($config['extend']['schema']['doctrine'][$entityClassName]['table'])) {
                 $tableName = $config['extend']['schema']['doctrine'][$entityClassName]['table'];
             } else {
                 $tableName = $this->metadataHelper->getTableNameByEntityClass($entityClassName);
             }
             // Process only existing tables
             if (!$toSchema->hasTable($tableName)) {
                 continue;
             }
             $table = $toSchema->getTable($tableName);
             $tableColumns = $table->getColumns();
             /**
              * Check if entity already has all needed columns.
              * If at least one is not present we should check and add it.
              */
             if (false === (bool) array_diff(array_keys(ActivityScope::$fieldsConfiguration), array_intersect(array_keys($tableColumns), array_keys(ActivityScope::$fieldsConfiguration)))) {
                 continue;
             }
             foreach (ActivityScope::$fieldsConfiguration as $fieldName => $fieldConfig) {
                 if (!$table->hasColumn($fieldName)) {
                     $hasSchemaChanges = true;
                     $table->addColumn($fieldName, $fieldConfig['type'], ['notnull' => false, OroOptions::KEY => array_merge([ExtendOptionsManager::MODE_OPTION => $fieldConfig['mode']], $fieldConfig['options'])]);
                 }
             }
         }
     }
     if ($hasSchemaChanges) {
         // Run schema related SQLs manually because this query run when diff is already calculated by schema tool
         $comparator = new Comparator();
         $platform = $this->connection->getDatabasePlatform();
         $schemaDiff = $comparator->compare($this->schema, $toSchema);
         foreach ($schemaDiff->toSql($platform) as $query) {
             $this->logQuery($logger, $query);
             if (!$dryRun) {
                 $this->connection->query($query);
             }
         }
     }
 }
コード例 #5
0
 /**
  * @param $config
  * @param $entityClassName
  *
  * @return string
  */
 protected function getTableName($config, $entityClassName)
 {
     if (isset($config['extend']['schema']['doctrine'][$entityClassName]['table'])) {
         $tableName = $config['extend']['schema']['doctrine'][$entityClassName]['table'];
     } else {
         $tableName = $this->metadataHelper->getTableNameByEntityClass($entityClassName);
     }
     return $tableName;
 }
コード例 #6
0
 /**
  * @param Schema   $schema
  * @param QueryBag $queries
  * @param string   $tableName
  * @param string   $columnName
  * @param array    $options
  */
 protected function processColumn(Schema $schema, QueryBag $queries, $tableName, $columnName, $options)
 {
     $className = $this->entityMetadataHelper->getEntityClassByTableName($tableName);
     if (null === $className) {
         return;
     }
     $table = $schema->getTable($tableName);
     if (!$table->hasColumn($columnName)) {
         return;
     }
     if (!isset($options[ExtendOptionsManager::NEW_NAME_OPTION])) {
         if (isset($options[ExtendOptionsManager::TYPE_OPTION])) {
             $this->buildIndex($columnName, $options, $className, $table);
         }
     } else {
         // in case of renaming column name we should rename existing index
         $this->renameIndex($schema, $queries, $tableName, $columnName, $options, $className, $table);
     }
 }
コード例 #7
0
 protected function renameOneToManyExtendField(Schema $schema, QueryBag $queries, Table $table, $associationName, $targetEntityClassName, EntityMetadataHelper $entityMetadataHelper)
 {
     $entityClassName = $entityMetadataHelper->getEntityClassByTableName($table->getName());
     $targetTableName = $entityMetadataHelper->getTableNameByEntityClass($targetEntityClassName);
     if ($schema->hasTable($targetTableName)) {
         $targetTable = $schema->getTable($targetTableName);
         $oldTargetColumnName = sprintf('field_%s_%s_id', strtolower(ExtendHelper::getShortClassName($entityClassName)), $associationName);
         if ($targetTable->hasColumn($oldTargetColumnName)) {
             $newTargetColumnName = $this->nameGenerator->generateOneToManyRelationColumnName($entityClassName, $associationName);
             $oldIndexName = $this->nameGenerator->generateIndexName($targetTableName, [$oldTargetColumnName], false, true);
             if ($targetTable->hasIndex($oldIndexName)) {
                 $targetTable->dropIndex($oldIndexName);
             }
             $oldForeignKeyName = $this->nameGenerator->generateForeignKeyConstraintName($targetTableName, [$oldTargetColumnName], true);
             if ($targetTable->hasForeignKey($oldForeignKeyName)) {
                 $targetTable->removeForeignKey($oldForeignKeyName);
             }
             $this->renameExtension->renameColumn($schema, $queries, $targetTable, $oldTargetColumnName, $newTargetColumnName);
             $this->renameExtension->addIndex($schema, $queries, $targetTable->getName(), [$newTargetColumnName]);
             $this->renameExtension->addForeignKeyConstraint($schema, $queries, $targetTable->getName(), $table->getName(), [$newTargetColumnName], $table->getPrimaryKeyColumns(), ['onDelete' => 'SET NULL']);
         }
     }
 }
コード例 #8
0
ファイル: ExtendExtension.php プロジェクト: hugeval/platform
 /**
  * Gets a field name by a table name and a column name
  *
  * @param string $tableName
  * @param string $columnName
  *
  * @return string|null
  */
 public function getFieldNameByColumnName($tableName, $columnName)
 {
     return $this->entityMetadataHelper->getFieldNameByColumnName($tableName, $columnName);
 }
コード例 #9
0
 /**
  * Gets an entity full class name by a table name
  *
  * @param string $tableName
  * @return string|null
  */
 public function getEntityClassByTableName($tableName)
 {
     return $this->entityMetadataHelper->getEntityClassByTableName($tableName);
 }
コード例 #10
0
 /**
  * Gets a field name by a column name
  *
  * @param string $tableName
  * @param string $columnName
  *
  * @return string
  */
 protected function getFieldName($tableName, $columnName)
 {
     $fieldName = $this->entityMetadataHelper->getFieldNameByColumnName($tableName, $columnName);
     return $fieldName ?: $columnName;
 }