From fe34a0d29feda2e57c1dd3149629af922dc9cb9b Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Mon, 8 Oct 2007 23:55:25 +0000 Subject: [PATCH] Fix to fixtures importing and relationships satisfying. --- lib/Doctrine/Data/Import.php | 7 ++++++- lib/Doctrine/Export.php | 21 ++++++++++++++++++++- lib/Doctrine/Import/Schema.php | 2 +- playground/index.php | 10 ++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/Data/Import.php b/lib/Doctrine/Data/Import.php index 694143674..127dd9268 100644 --- a/lib/Doctrine/Data/Import.php +++ b/lib/Doctrine/Data/Import.php @@ -127,6 +127,7 @@ class Doctrine_Data_Import extends Doctrine_Data } } + // Satisfy all relationships foreach ($pendingRelations as $rowKey => $pending) { $obj = $pending['obj']; $key = $pending['key']; @@ -134,7 +135,11 @@ class Doctrine_Data_Import extends Doctrine_Data $foreign = $pending['foreign']; $pks = $primaryKeys[$key]; $obj->$local = $pks['id']; - + } + + // Loop over all again to save them since we satisfied all pending relationships above + foreach ($pendingRelations as $rowKey => $pending) { + $obj = $pending['obj']; $obj->save(); } } diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 3c3f6a6e4..a6efeacd6 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1000,12 +1000,30 @@ class Doctrine_Export extends Doctrine_Connection_Module if (!isset($connections[$connectionName])) { $connections[$connectionName] = array(); + $connections[$connectionName]['creates'] = array(); + $connections[$connectionName]['alters'] = array(); } - $connections[$connectionName] = array_merge($connections[$connectionName], $this->exportClassesSql(array($class))); + $sql = $this->exportClassesSql(array($class)); + // The create sql query is the first one, and everything else is the alters + $create = $sql[0]; + + // Remove create from the main array + unset($sql[0]); + + // Store the creates and alters individually so we can merge them back together later + // We need the creates to happen first, then the alters + $connections[$connectionName]['creates'][] = $create; + $connections[$connectionName]['alters'] = array_merge($connections[$connectionName]['alters'], $sql); } + // Loop over all the sql again to merge the creates and alters in to the same array, but so that the alters are at the bottom + $build = array(); foreach ($connections as $connectionName => $sql) { + $build[$connectionName] = array_merge($sql['creates'], $sql['alters']); + } + + foreach ($build as $connectionName => $sql) { $connection = Doctrine_Manager::getInstance()->getConnection($connectionName); $connection->beginTransaction(); @@ -1016,6 +1034,7 @@ class Doctrine_Export extends Doctrine_Connection_Module } catch (Doctrine_Connection_Exception $e) { // we only want to silence table already exists errors if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { + echo $query."\n"; $connection->rollback(); throw $e; } diff --git a/lib/Doctrine/Import/Schema.php b/lib/Doctrine/Import/Schema.php index 5e39c7612..5e96c13c0 100644 --- a/lib/Doctrine/Import/Schema.php +++ b/lib/Doctrine/Import/Schema.php @@ -138,7 +138,7 @@ class Doctrine_Import_Schema $columns = array(); $className = isset($table['className']) ? (string) $table['className']:(string) $className; - $tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $className; + $tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) Doctrine::tableize($className); $build[$className]['className'] = $className; diff --git a/playground/index.php b/playground/index.php index 32fbef2d2..4ede7d294 100644 --- a/playground/index.php +++ b/playground/index.php @@ -6,7 +6,17 @@ $conn = Doctrine_Manager::connection($dbh); $manager = Doctrine_Manager::getInstance(); $manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL); +// Build models from schema +//$import = new Doctrine_Import_Schema(); +//$import->generateBaseClasses(true); +//$import->importSchema('schema.yml', 'yml', 'test_models'); + +// Export models schema to database +//Doctrine::exportSchema('test_models'); + +// Load model classes Doctrine::loadModels('test_models'); +// Load data fixtures $data = new Doctrine_Data(); $data->importData('fixtures.yml'); \ No newline at end of file