Skip to content

Commit d31286d

Browse files
committed
support autoincrement and primary key
1 parent e45c8b1 commit d31286d

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

example/example.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<schema>
3-
<table name="user">
4-
<column name="id" type="INT"/>
3+
<table name="user" primaryKey="id">
4+
<column name="id" type="INT" autoincrement="true"/>
55
<column name="name" type="VARCHAR(32)"/>
66
<column name="display_name" type="VARCHAR(64)"/>
77
<column name="about" type="LONGTEXT"/>

src/Command/SchemaLoadCommand.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,42 @@ function SchemaLoadCommand($input, $output)
2323
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
2424

2525
$toSchema = new \Doctrine\DBAL\Schema\Schema();
26-
26+
2727
$x = \Doctrine\DBAL\Types\Type::getTypesMap();
2828

29-
29+
3030
$xml = simplexml_load_file($filename);
31-
31+
3232
foreach ($xml->table as $tableNode) {
3333
//echo ;
3434
//echo $child->getName() . ": " . $child . "<br>";
3535
$table = $toSchema->createTable((string)$tableNode['name']);
3636

37-
/*
38-
$table->addColumn('id', 'integer', array("unsigned" => true, 'autoincrement' => true));
39-
$table->setPrimaryKey(array("id"));
40-
*/
37+
// $table->addColumn('id', 'integer', array("unsigned" => true, 'autoincrement' => true));
38+
// $table->setPrimaryKey(array("id"));
4139

4240
foreach ($tableNode->column as $columnNode) {
4341
$name = $columnNode['name'];
4442
$srctype = $columnNode['type'];
45-
43+
$autoincrement = ($columnNode['autoincrement'] == 'true');
44+
4645
$type = trim($srctype, ' )');
4746
$part = explode('(', $type);
4847
$options = array();
4948
switch(strtolower($part[0])) {
5049
case 'bigint':
5150
$type = 'bigint';
5251
break;
53-
52+
5453
case 'tinyint':
5554
$type = 'boolean';
5655
break;
57-
56+
5857
case 'integer':
5958
case 'int':
6059
$type= 'integer';
6160
break;
62-
61+
6362
case "longtext":
6463
case "text":
6564
$type = 'text';
@@ -82,10 +81,19 @@ function SchemaLoadCommand($input, $output)
8281
throw new RuntimeException("Unsupported type: " . $srctype);
8382
}
8483
//echo "Creating $name of `$type` - `$srctype`\n";
84+
if ($autoincrement) {
85+
$options['autoincrement'] = true;
86+
}
8587

8688
$table->addColumn($name, $type, $options);
8789
}
88-
90+
91+
// only if specified
92+
if ((string) $xml['primaryKey']) {
93+
$table->setPrimaryKey(
94+
[(string) $xml['primaryKey']]
95+
);
96+
}
8997
}
9098

9199
$platform = $conn->getDatabasePlatform();
@@ -95,7 +103,7 @@ function SchemaLoadCommand($input, $output)
95103

96104
$sm = $conn->getSchemaManager();
97105
$fromSchema = $sm->createSchema();
98-
106+
99107
$comparator = new \Doctrine\DBAL\Schema\Comparator();
100108
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
101109

0 commit comments

Comments
 (0)