From 5bb1dbb893f3c0cc9fbb635261f766ba6989f22f Mon Sep 17 00:00:00 2001 From: zYne Date: Tue, 5 Jun 2007 20:55:54 +0000 Subject: [PATCH] --- lib/Doctrine/Export.php | 70 +++++++++++++++++++++++++++++++++++++++- lib/Doctrine/Table.php | 71 +++++++---------------------------------- 2 files changed, 80 insertions(+), 61 deletions(-) diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index 50397a15a..c89d926b2 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -804,8 +804,76 @@ class Doctrine_Export extends Doctrine_Connection_Module } $conn->setAttribute(Doctrine::ATTR_CREATE_TABLES, $old); } - public function exportTable() + /** + * exportTable + * exports given table into database based on column and option definitions + * + * @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS + * occurred during the create table operation + * @return boolean whether or not the export operation was successful + * false if table already existed in the database + */ + public function exportTable(Doctrine_Table $table) { + if ( ! Doctrine::isValidClassname($table->getOption('declaringClass')->getName())) { + throw new Doctrine_Export_Exception('Class name not valid.'); + } + try { + $columns = array(); + $primary = array(); + + foreach ($table->getColumns() as $name => $column) { + $definition = $column[2]; + $definition['type'] = $column[0]; + $definition['length'] = $column[1]; + + switch ($definition['type']) { + case 'enum': + if (isset($definition['default'])) { + $definition['default'] = $table->enumIndex($name, $definition['default']); + } + break; + case 'boolean': + if (isset($definition['default'])) { + $definition['default'] = $table->getConnection()->convertBooleans($definition['default']); + } + break; + } + $columns[$name] = $definition; + + if(isset($definition['primary']) && $definition['primary']) { + $primary[] = $name; + } + } + + if ($table->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS) { + + foreach ($table->getRelations() as $name => $relation) { + $fk = $relation->toArray(); + $fk['foreignTable'] = $relation->getTable()->getTableName(); + + if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) { + continue; + } + + if ($relation->hasConstraint()) { + + $options['foreignKeys'][] = $fk; + } elseif ($relation instanceof Doctrine_Relation_LocalKey) { + $options['foreignKeys'][] = $fk; + } + } + } + + $options['primary'] = $primary; + + $this->conn->export->createTable($table->getOption('tableName'), $columns, array_merge($table->getOptions(), $options)); + } catch(Doctrine_Connection_Exception $e) { + // we only want to silence table already exists errors + if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { + throw $e; + } + } } } diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 138f19088..84c6c2d50 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -308,66 +308,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable */ public function export() { - if ( ! Doctrine::isValidClassname($this->options['declaringClass']->getName())) { - throw new Doctrine_Table_Exception('Class name not valid.'); - } - - try { - $columns = array(); - $primary = array(); - - foreach ($this->columns as $name => $column) { - $definition = $column[2]; - $definition['type'] = $column[0]; - $definition['length'] = $column[1]; - - switch ($definition['type']) { - case 'enum': - if (isset($definition['default'])) { - $definition['default'] = $this->enumIndex($name, $definition['default']); - } - break; - case 'boolean': - if (isset($definition['default'])) { - $definition['default'] = $this->conn->convertBooleans($definition['default']); - } - break; - } - $columns[$name] = $definition; - - if(isset($definition['primary']) && $definition['primary']) { - $primary[] = $name; - } - } - - if ($this->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS) { - - foreach ($this->getRelations() as $name => $relation) { - $fk = $relation->toArray(); - $fk['foreignTable'] = $relation->getTable()->getTableName(); - - if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) { - continue; - } - - if ($relation->hasConstraint()) { - - $options['foreignKeys'][] = $fk; - } elseif ($relation instanceof Doctrine_Relation_LocalKey) { - $options['foreignKeys'][] = $fk; - } - } - } - - $options['primary'] = $primary; - - $this->conn->export->createTable($this->options['tableName'], $columns, array_merge($this->options, $options)); - } catch(Doctrine_Connection_Exception $e) { - // we only want to silence table already exists errors - if($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { - throw $e; - } - } + $this->conn->export->exportTable($this); } /** * exportConstraints @@ -423,6 +364,16 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { return isset($this->options[$option]); } + /** + * getOptions + * returns all options of this table and the associated values + * + * @return array all options and their values + */ + public function getOptions() + { + return $this->options; + } /** * addForeignKey *