This commit is contained in:
parent
1b807d8e97
commit
1cfe7fc65a
@ -66,7 +66,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
*/
|
*/
|
||||||
public function dropIndex($table, $name)
|
public function dropIndex($table, $name)
|
||||||
{
|
{
|
||||||
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
||||||
return $this->conn->exec('DROP INDEX ' . $name);
|
return $this->conn->exec('DROP INDEX ' . $name);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -79,8 +79,8 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
*/
|
*/
|
||||||
public function dropConstraint($table, $name, $primary = false)
|
public function dropConstraint($table, $name, $primary = false)
|
||||||
{
|
{
|
||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table);
|
||||||
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
||||||
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name);
|
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $name);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -142,6 +142,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
if (empty($fields)) {
|
if (empty($fields)) {
|
||||||
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
throw new Doctrine_Export_Exception('no fields specified for table ' . $name);
|
||||||
}
|
}
|
||||||
|
|
||||||
$queryFields = $this->getFieldDeclarationList($fields);
|
$queryFields = $this->getFieldDeclarationList($fields);
|
||||||
|
|
||||||
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
|
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
|
||||||
@ -225,19 +226,22 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
*/
|
*/
|
||||||
public function createConstraint($table, $name, $definition)
|
public function createConstraint($table, $name, $definition)
|
||||||
{
|
{
|
||||||
$table = $this->conn->quoteIdentifier($table, true);
|
$table = $this->conn->quoteIdentifier($table);
|
||||||
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
|
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
|
||||||
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
|
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
|
||||||
if (!empty($definition['primary'])) {
|
|
||||||
|
if (isset($definition['primary']) && $definition['unique']) {
|
||||||
$query .= ' PRIMARY KEY';
|
$query .= ' PRIMARY KEY';
|
||||||
} elseif (!empty($definition['unique'])) {
|
} elseif (isset($definition['unique']) && $definition['unique']) {
|
||||||
$query .= ' UNIQUE';
|
$query .= ' UNIQUE';
|
||||||
}
|
}
|
||||||
|
|
||||||
$fields = array();
|
$fields = array();
|
||||||
foreach (array_keys($definition['fields']) as $field) {
|
foreach (array_keys($definition['fields']) as $field) {
|
||||||
$fields[] = $this->conn->quoteIdentifier($field, true);
|
$fields[] = $this->conn->quoteIdentifier($field, true);
|
||||||
}
|
}
|
||||||
$query .= ' ('. implode(', ', $fields) . ')';
|
$query .= ' ('. implode(', ', $fields) . ')';
|
||||||
|
|
||||||
return $this->conn->exec($query);
|
return $this->conn->exec($query);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -311,15 +315,19 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* createForeignKey
|
* createForeignKeySql
|
||||||
*
|
*
|
||||||
* @param string $table name of the table on which the index is to be created
|
* @param string $table name of the table on which the foreign key is to be created
|
||||||
* @param string $name name of the foreign key to be created
|
|
||||||
* @param array $definition associative array that defines properties of the foreign key to be created.
|
* @param array $definition associative array that defines properties of the foreign key to be created.
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function createForeignKey($table, $name, array $definition)
|
public function createForeignKeySql($table, array $definition)
|
||||||
{
|
{
|
||||||
|
$table = $this->conn->quoteIdentifier($table);
|
||||||
|
|
||||||
|
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $this->getForeignKeyDeclaration($definition);
|
||||||
|
|
||||||
|
return $query;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* alter an existing table
|
* alter an existing table
|
||||||
@ -719,7 +727,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
{
|
{
|
||||||
$sql = '';
|
$sql = '';
|
||||||
if (isset($definition['name'])) {
|
if (isset($definition['name'])) {
|
||||||
$sql .= 'CONSTRAINT ' . $definition['name'] . ' ';
|
$sql .= 'CONSTRAINT ' . $this->conn->quoteIdentifier($definition['name']) . ' ';
|
||||||
}
|
}
|
||||||
$sql .= 'FOREIGN KEY (';
|
$sql .= 'FOREIGN KEY (';
|
||||||
|
|
||||||
@ -785,24 +793,116 @@ class Doctrine_Export extends Doctrine_Connection_Module
|
|||||||
* export
|
* export
|
||||||
* method for exporting Doctrine_Record classes to a schema
|
* method for exporting Doctrine_Record classes to a schema
|
||||||
*
|
*
|
||||||
|
* if the directory parameter is given this method first iterates
|
||||||
|
* recursively trhough the given directory in order to find any model classes
|
||||||
|
*
|
||||||
|
* Then it iterates through all declared classes and creates tables for the ones
|
||||||
|
* that extend Doctrine_Record and are not abstract classes
|
||||||
|
*
|
||||||
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
||||||
|
* occurred during the create table operation
|
||||||
|
* @param string $directory optional directory parameter
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function exportAll()
|
public function export($directory = null)
|
||||||
{
|
{
|
||||||
$parent = new ReflectionClass('Doctrine_Record');
|
if ($directory !== null) {
|
||||||
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
|
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory),
|
||||||
$old = $conn->getAttribute(Doctrine::ATTR_CREATE_TABLES);
|
RecursiveIteratorIterator::LEAVES_ONLY);
|
||||||
|
|
||||||
$conn->setAttribute(Doctrine::ATTR_CREATE_TABLES, true);
|
foreach ($it as $file) {
|
||||||
|
$e = explode('.', $file->getFileName());
|
||||||
|
if (end($e) === 'php' && count($e) === 2) {
|
||||||
|
require_once $e->getPathName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = new ReflectionClass('Doctrine_Record');
|
||||||
|
|
||||||
foreach (get_declared_classes() as $name) {
|
foreach (get_declared_classes() as $name) {
|
||||||
$class = new ReflectionClass($name);
|
$class = new ReflectionClass($name);
|
||||||
|
$conn = Doctrine_Manager::getInstance()->getConnectionForComponent($name);
|
||||||
|
|
||||||
if ($class->isSubclassOf($parent) && ! $class->isAbstract()) {
|
if ($class->isSubclassOf($parent) && ! $class->isAbstract()) {
|
||||||
$obj = new $class();
|
$record = new $name();
|
||||||
|
$table = $record->getTable();
|
||||||
|
|
||||||
|
$conn->export->exportTable($table);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$conn->setAttribute(Doctrine::ATTR_CREATE_TABLES, $old);
|
}
|
||||||
|
/**
|
||||||
|
* export
|
||||||
|
* returns the sql for exporting Doctrine_Record classes to a schema
|
||||||
|
*
|
||||||
|
* if the directory parameter is given this method first iterates
|
||||||
|
* recursively trhough the given directory in order to find any model classes
|
||||||
|
*
|
||||||
|
* Then it iterates through all declared classes and creates tables for the ones
|
||||||
|
* that extend Doctrine_Record and are not abstract classes
|
||||||
|
*
|
||||||
|
* @throws Doctrine_Connection_Exception if some error other than Doctrine::ERR_ALREADY_EXISTS
|
||||||
|
* occurred during the create table operation
|
||||||
|
* @param string $directory optional directory parameter
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function exportSql($directory = null)
|
||||||
|
{
|
||||||
|
$declared = get_declared_classes();
|
||||||
|
|
||||||
|
if ($directory !== null) {
|
||||||
|
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory),
|
||||||
|
RecursiveIteratorIterator::LEAVES_ONLY);
|
||||||
|
|
||||||
|
foreach ($it as $file) {
|
||||||
|
$e = explode('.', $file->getFileName());
|
||||||
|
if (end($e) === 'php' && count($e) === 2) {
|
||||||
|
require_once $file->getPathName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = new ReflectionClass('Doctrine_Record');
|
||||||
|
|
||||||
|
$sql = array();
|
||||||
|
$fks = array();
|
||||||
|
|
||||||
|
// we iterate trhough the diff of previously declared classes
|
||||||
|
// and currently declared classes
|
||||||
|
foreach (array_diff(get_declared_classes(), $declared) as $name) {
|
||||||
|
$class = new ReflectionClass($name);
|
||||||
|
$conn = Doctrine_Manager::getInstance()->getConnectionForComponent($name);
|
||||||
|
|
||||||
|
// check if class is an instance of Doctrine_Record and not abstract
|
||||||
|
if ($class->isSubclassOf($parent) && ! $class->isAbstract()) {
|
||||||
|
$record = new $name();
|
||||||
|
$table = $record->getTable();
|
||||||
|
$data = $table->getExportableFormat();
|
||||||
|
|
||||||
|
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
|
||||||
|
|
||||||
|
if (is_array($query)) {
|
||||||
|
$sql = array_merge($sql, $query);
|
||||||
|
} else {
|
||||||
|
$sql[] = $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($data['options']['foreignKeys']) && is_array($data['options']['foreignKeys'])) {
|
||||||
|
$fks[$table->getTableName()] = $data['options']['foreignKeys'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($fks as $tableName => $fk) {
|
||||||
|
foreach ($fk as $k => $definition) {
|
||||||
|
if (is_array($definition)) {
|
||||||
|
|
||||||
|
$sql[] = $this->createForeignKeySql($definition['table'], $definition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* exportTable
|
* exportTable
|
||||||
|
Loading…
x
Reference in New Issue
Block a user