1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Fix to fixtures importing and relationships satisfying.

This commit is contained in:
Jonathan.Wage 2007-10-08 23:55:25 +00:00
parent c69c0c5d53
commit fe34a0d29f
4 changed files with 37 additions and 3 deletions

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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');