Commit fe34a0d2 authored by Jonathan.Wage's avatar Jonathan.Wage

Fix to fixtures importing and relationships satisfying.

parent c69c0c5d
...@@ -127,6 +127,7 @@ class Doctrine_Data_Import extends Doctrine_Data ...@@ -127,6 +127,7 @@ class Doctrine_Data_Import extends Doctrine_Data
} }
} }
// Satisfy all relationships
foreach ($pendingRelations as $rowKey => $pending) { foreach ($pendingRelations as $rowKey => $pending) {
$obj = $pending['obj']; $obj = $pending['obj'];
$key = $pending['key']; $key = $pending['key'];
...@@ -134,7 +135,11 @@ class Doctrine_Data_Import extends Doctrine_Data ...@@ -134,7 +135,11 @@ class Doctrine_Data_Import extends Doctrine_Data
$foreign = $pending['foreign']; $foreign = $pending['foreign'];
$pks = $primaryKeys[$key]; $pks = $primaryKeys[$key];
$obj->$local = $pks['id']; $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(); $obj->save();
} }
} }
......
...@@ -1000,12 +1000,30 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1000,12 +1000,30 @@ class Doctrine_Export extends Doctrine_Connection_Module
if (!isset($connections[$connectionName])) { if (!isset($connections[$connectionName])) {
$connections[$connectionName] = array(); $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) { 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 = Doctrine_Manager::getInstance()->getConnection($connectionName);
$connection->beginTransaction(); $connection->beginTransaction();
...@@ -1016,6 +1034,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1016,6 +1034,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
} catch (Doctrine_Connection_Exception $e) { } catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors // we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
echo $query."\n";
$connection->rollback(); $connection->rollback();
throw $e; throw $e;
} }
......
...@@ -138,7 +138,7 @@ class Doctrine_Import_Schema ...@@ -138,7 +138,7 @@ class Doctrine_Import_Schema
$columns = array(); $columns = array();
$className = isset($table['className']) ? (string) $table['className']:(string) $className; $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; $build[$className]['className'] = $className;
......
...@@ -6,7 +6,17 @@ $conn = Doctrine_Manager::connection($dbh); ...@@ -6,7 +6,17 @@ $conn = Doctrine_Manager::connection($dbh);
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL); $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'); Doctrine::loadModels('test_models');
// Load data fixtures
$data = new Doctrine_Data(); $data = new Doctrine_Data();
$data->importData('fixtures.yml'); $data->importData('fixtures.yml');
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment