1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Fixes for exporting sql to correct connections.

This commit is contained in:
Jonathan.Wage 2007-10-05 05:06:42 +00:00
parent d2d52a63ce
commit 956c5dfe91
2 changed files with 55 additions and 36 deletions

View File

@ -976,23 +976,10 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return void
*/
public function exportSchema($directory = null)
{
$sql = $this->exportSql($directory);
$this->conn->beginTransaction();
foreach ($sql as $query) {
try {
$this->conn->exec($query);
} catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$this->conn->rollback();
throw $e;
}
}
}
$this->conn->commit();
{
$models = Doctrine::loadModels($directory);
$this->exportClasses($models);
}
/**
* exportClasses
@ -1005,22 +992,38 @@ class Doctrine_Export extends Doctrine_Connection_Module
*/
public function exportClasses(array $classes)
{
$sql = $this->exportClassesSql($classes);
$this->conn->beginTransaction();
foreach ($sql as $query) {
try {
$this->conn->exec($query);
} catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$this->conn->rollback();
throw $e;
$connections = array();
foreach ($classes as $class) {
$record = new $class();
$connection = $record->getTable()->getConnection();
$connectionName = Doctrine_Manager::getInstance()->getConnectionName($connection);
if (!isset($connections[$connectionName])) {
$connections[$connectionName] = array();
}
$connections[$connectionName] = array_merge($connections[$connectionName], $this->exportClassesSql(array($class)));
}
foreach ($connections as $connectionName => $sql) {
$connection = Doctrine_Manager::getInstance()->getConnection($connectionName);
$connection->beginTransaction();
foreach ($sql as $query) {
try {
$connection->exec($query);
} catch (Doctrine_Connection_Exception $e) {
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$connection->rollback();
throw $e;
}
}
}
$connection->commit();
}
$this->conn->commit();
}
/**
* exportClassesSql
@ -1050,6 +1053,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
} else {
$sql[] = $query;
}
if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_PLUGINS) {
$sql = array_merge($sql, $this->exportPluginsSql($table));
}

View File

@ -134,6 +134,11 @@ END;
$i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) {
$ret[$i] = "\t\t\t\tparent::setTableDefinition();";
$i++;
}
if (isset($options['tableName']) && !empty($options['tableName'])) {
$ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');';
@ -188,7 +193,7 @@ END;
}
if (!empty($ret)) {
return "\n\t\tpublic function setTableDefinition()"."\n\t\t{\n\t\t\t\tparent::setTableDefinition();\n".implode("\n", $ret)."\n\t\t}";
return "\n\t\tpublic function setTableDefinition()"."\n\t\t{\n".implode("\n", $ret)."\n\t\t}";
}
}
public function buildSetUp(array $options, array $columns, array $relations)
@ -197,6 +202,11 @@ END;
$i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) {
$ret[$i] = "\t\t\t\tparent::setUp();";
$i++;
}
foreach ($relations as $name => $relation) {
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
@ -253,7 +263,7 @@ END;
}
if (!empty($ret)) {
return "\n\t\tpublic function setUp()\n\t\t{\n\t\t\t\tparent::setUp();\n\t\t\t\t".implode("\n", $ret)."\n\t\t}";
return "\n\t\tpublic function setUp()\n\t\t{\n".implode("\n", $ret)."\n\t\t}";
}
}
@ -266,8 +276,13 @@ END;
$className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
$definition = $this->buildTableDefinition($options, $columns, $relations);
$setUp = $this->buildSetUp($options, $columns, $relations);
if (!isset($options['no_definition'])) {
$definition = $this->buildTableDefinition($options, $columns, $relations);
$setUp = $this->buildSetUp($options, $columns, $relations);
} else {
$definition = null;
$setUp = null;
}
$content = sprintf(self::$tpl, $className,
$extends,
@ -298,7 +313,7 @@ END;
if ($this->generateBaseClasses()) {
if (!file_exists($options['fileName'])) {
//if (!file_exists($options['fileName'])) {
$optionsBak = $options;
unset($options['tableName']);
@ -306,7 +321,7 @@ END;
$this->writeDefinition($options, array(), array());
$options = $optionsBak;
}
//}
$generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;