Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[wip] Pr mapping import #659

Closed
wants to merge 2 commits into from

3 participants

@bronze1man

The bug:

1.use symfony2 doctrine:mapping:import from msyql database to annotation
2.use symfony2 doctrine:schema:update will see a lot of sql update about just import database schema.
3.this PR fix eliminate some of those sql update...

fix 8 bug:

  • column default
  • column unsigned
  • column type char
  • table without auto_increment
  • column comment
  • column type decimal
  • table with simple index
  • table with unique index

wip -> follow rule of contribute to doctrine

  • add patch on master
  • add test
@bronze1man

two more bug forget to mention

  • table with simple index
  • table with unique index
@Ocramius
Owner

@bronze1man bug fixes need tests that demonstrate the failures!

@bronze1man

1.use symfony2 doctrine:mapping:import from msyql database to annotation
2.use symfony2 doctrine:schema:update will see a lot of sql update about just import database schema.
3.this PR fix eliminate some of those sql update...

@bronze1man bronze1man deleted the bronze1man:PR-mapping-import branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
36 lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php
@@ -168,7 +168,18 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$metadata->table['name'] = $tableName;
$columns = $this->tables[$tableName]->getColumns();
+ /** @var Doctrine\DBAL\Schema\Index $indexes */
$indexes = $this->tables[$tableName]->getIndexes();
+ foreach($indexes as $index){
+ if ($index->isPrimary()){
+ continue;
+ }
+ if ($index->isSimpleIndex()){
+ $metadata->table['indexes'][$index->getName()]['columns'] = $index->getColumns();
+ }else if($index->isUnique()){
+ $metadata->table['uniqueConstraints'][$index->getName()]['columns'] = $index->getColumns();
+ }
+ }
try {
$primaryKeyColumns = $this->tables[$tableName]->getPrimaryKey()->getColumns();
} catch(SchemaException $e) {
@@ -188,6 +199,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$ids = array();
$fieldMappings = array();
+ $is_auto_increment = false;
foreach ($columns as $column) {
$fieldMapping = array();
@@ -204,11 +216,23 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
if ($column->getType() instanceof \Doctrine\DBAL\Types\StringType) {
$fieldMapping['length'] = $column->getLength();
$fieldMapping['fixed'] = $column->getFixed();
- } else if ($column->getType() instanceof \Doctrine\DBAL\Types\IntegerType) {
- $fieldMapping['unsigned'] = $column->getUnsigned();
+ }else if ($column->getType() instanceof \Doctrine\DBAL\Types\DecimalType){
+ $fieldMapping['precision'] = $column->getPrecision();
+ $fieldMapping['scale'] = $column->getScale();
}
+
+ $fieldMapping['unsigned'] = $column->getUnsigned();
$fieldMapping['nullable'] = $column->getNotNull() ? false : true;
+ $fieldMapping['default'] = $column->getDefault();
+ $comment = $column->getComment();
+ if (!empty($comment)){
+ $fieldMapping['comment'] = $comment;
+ }
+ if ($column->getAutoincrement()){
+ $is_auto_increment = true;
+ }
+
if (isset($fieldMapping['id'])) {
$ids[] = $fieldMapping;
} else {
@@ -218,14 +242,17 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
if ($ids) {
if (count($ids) == 1) {
- $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
+ if ($is_auto_increment){
+ $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
+ }else{
+ $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
+ }
}
foreach ($ids as $id) {
$metadata->mapField($id);
}
}
-
foreach ($fieldMappings as $fieldMapping) {
$metadata->mapField($fieldMapping);
}
@@ -319,6 +346,7 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
$metadata->mapManyToOne($associationMapping);
}
}
+
}
/**
View
18 lib/Doctrine/ORM/Tools/EntityGenerator.php
@@ -1197,6 +1197,24 @@ private function generateFieldMappingPropertyDocBlock(array $fieldMapping, Class
$column[] = 'unique=' . var_export($fieldMapping['unique'], true);
}
+ $options = array();
+ if (!empty($fieldMapping['unsigned'])) {
+ $options[] = '"unsigned"=true';
+ }
+ if (isset($fieldMapping['default'])){
+ $options[] = '"default"=' .json_encode($fieldMapping['default']);
+ }
+ if (!empty($fieldMapping['fixed'])){
+ $options[] = '"fixed"=' .json_encode($fieldMapping['fixed']);
+ }
+ if (!empty($fieldMapping['comment'])){
+ $options[] = '"comment"="' .$fieldMapping['comment'].'"';
+ }
+ if (!empty($options)){
+ $column[] = 'options={'.implode(',',$options).'}';
+ }
+
+
$lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Column(' . implode(', ', $column) . ')';
if (isset($fieldMapping['id']) && $fieldMapping['id']) {
View
6 lib/Doctrine/ORM/Tools/SchemaTool.php
@@ -395,6 +395,12 @@ private function _gatherColumn($class, array $mapping, $table)
}
}
+ if (isset($mapping['options']['default'])) {
+ $options['default'] = $mapping['options']['default'];
+
+ unset($mapping['options']['default']);
+ }
+
$options['customSchemaOptions'] = $mapping['options'];
}
Something went wrong with that request. Please try again.