diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index c21980f03..3c3f6a6e4 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -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)); } diff --git a/lib/Doctrine/Import/Builder.php b/lib/Doctrine/Import/Builder.php index b6e3172bb..1a7980fb2 100644 --- a/lib/Doctrine/Import/Builder.php +++ b/lib/Doctrine/Import/Builder.php @@ -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;