diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 4847bc5de..8d857a62a 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -144,9 +144,16 @@ class Doctrine_Export extends Doctrine_Connection_Module } $queryFields = $this->getFieldDeclarationList($fields); + if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) { + foreach($options['foreignKeys'] as $definition) { + $queryFields .= ', ' . $this->getForeignKeyDeclaration($definition); + } + } + if (isset($options['primary']) && ! empty($options['primary'])) { $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')'; } + if (isset($options['indexes']) && ! empty($options['indexes'])) { foreach($options['indexes'] as $index => $definition) { $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); diff --git a/lib/Doctrine/Export/Sqlite.php b/lib/Doctrine/Export/Sqlite.php index a1c2ba851..7f9f617bb 100644 --- a/lib/Doctrine/Export/Sqlite.php +++ b/lib/Doctrine/Export/Sqlite.php @@ -157,6 +157,13 @@ class Doctrine_Export_Sqlite extends Doctrine_Export if ( ! $autoinc && isset($options['primary']) && ! empty($options['primary'])) { $queryFields.= ', PRIMARY KEY('.implode(', ', array_values($options['primary'])).')'; } + + // sqlite doesn't support foreign key declaration but it parses those anyway + if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) { + foreach($options['foreignKeys'] as $definition) { + $queryFields .= ', ' . $this->getForeignKeyDeclaration($definition); + } + } if (isset($options['indexes']) && ! empty($options['indexes'])) { foreach($options['indexes'] as $index => $definition) { diff --git a/tests/Export/MysqlTestCase.php b/tests/Export/MysqlTestCase.php index 674f9239c..bbbb50991 100644 --- a/tests/Export/MysqlTestCase.php +++ b/tests/Export/MysqlTestCase.php @@ -263,16 +263,16 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase public function testExportSupportsForeignKeys() { - $r = new MysqlForeignKeyTest; + $r = new ForeignKeyTest; - $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES mysql_foreign_key_test(id), FOREIGN KEY id REFERENCES mysql_foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB'); + $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id BIGINT AUTO_INCREMENT, name TEXT, code INT, content TEXT, parent_id BIGINT, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE, PRIMARY KEY(id)) ENGINE = INNODB'); } public function testExportSupportsForeignKeysWithoutAttributes() { - $r = new MysqlForeignKeyTest2; + $r = new ForeignKeyTest2; - $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mysql_foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES mysql_foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB'); + $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test2 (id BIGINT AUTO_INCREMENT, name TEXT, foreignkey BIGINT, FOREIGN KEY foreignkey REFERENCES foreign_key_test(id), PRIMARY KEY(id)) ENGINE = INNODB'); } public function testExportSupportsForeignKeysForManyToManyRelations() @@ -290,7 +290,7 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_UnitTestCase } } -class MysqlForeignKeyTest extends Doctrine_Record +class ForeignKeyTest extends Doctrine_Record { public function setTableDefinition() { @@ -299,12 +299,12 @@ class MysqlForeignKeyTest extends Doctrine_Record $this->hasColumn('content', 'string', 4000); $this->hasColumn('parent_id', 'integer'); - $this->hasOne('MysqlForeignKeyTest as Parent', - 'MysqlForeignKeyTest.parent_id' + $this->hasOne('ForeignKeyTest as Parent', + 'ForeignKeyTest.parent_id' ); - $this->hasMany('MysqlForeignKeyTest as Children', - 'MysqlForeignKeyTest.parent_id', + $this->hasMany('ForeignKeyTest as Children', + 'ForeignKeyTest.parent_id', array('onDelete' => 'CASCADE', 'onUpdate' => 'RESTRICT') ); @@ -339,14 +339,14 @@ class MysqlGroup extends Doctrine_Record $this->hasMany('MysqlUser', 'MysqlGroupMember.user_id'); } } -class MysqlForeignKeyTest2 extends Doctrine_Record +class ForeignKeyTest2 extends Doctrine_Record { public function setTableDefinition() { $this->hasColumn('name', 'string', null); $this->hasColumn('foreignkey', 'integer'); - $this->hasOne('MysqlForeignKeyTest', 'MysqlForeignKeyTest2.foreignkey'); + $this->hasOne('ForeignKeyTest', 'ForeignKeyTest2.foreignkey'); } } class MysqlIndexTestRecord extends Doctrine_Record diff --git a/tests/Export/SqliteTestCase.php b/tests/Export/SqliteTestCase.php index 017dc65e5..3c13664bc 100644 --- a/tests/Export/SqliteTestCase.php +++ b/tests/Export/SqliteTestCase.php @@ -46,7 +46,7 @@ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($this->adapter->pop(), 'CREATE TABLE mytable (name CHAR(10), type INTEGER, PRIMARY KEY(name, type))'); } - public function testCreateTableSupportsIndexes() + public function testCreateTableSupportsIndexes() { $fields = array('id' => array('type' => 'integer', 'unsigned' => 1, 'autoincrement' => true, 'unique' => true), 'name' => array('type' => 'string', 'length' => 4), @@ -91,5 +91,11 @@ class Doctrine_Export_Sqlite_TestCase extends Doctrine_UnitTestCase { $this->assertEqual($this->adapter->pop(), 'CREATE TABLE sometable (id INTEGER UNSIGNED PRIMARY KEY AUTOINCREMENT, name VARCHAR(4), INDEX myindex (id ASC, name DESC))'); } + public function testExportSupportsForeignKeys() + { + $r = new ForeignKeyTest; + + $this->assertEqual($this->adapter->pop(), 'CREATE TABLE foreign_key_test (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(2147483647), code INTEGER, content VARCHAR(4000), parent_id INTEGER, FOREIGN KEY parent_id REFERENCES foreign_key_test(id), FOREIGN KEY id REFERENCES foreign_key_test(parent_id) ON UPDATE RESTRICT ON DELETE CASCADE)'); + } } ?>