Commit c9658cb2 authored by Jonathan.Wage's avatar Jonathan.Wage

Getting exporting/importing working.

parent 13d781cb
...@@ -57,7 +57,49 @@ abstract class Doctrine_Export_Schema ...@@ -57,7 +57,49 @@ abstract class Doctrine_Export_Schema
* @param string $schema * @param string $schema
* @return void * @return void
*/ */
abstract function dump($array, $schema); public function dump($array, $schema)
{
$data = $this->build($array);
file_put_contents($schema, $data);
}
public function getDirectoryTables($directory)
{
$parent = new ReflectionClass('Doctrine_Record');
$declared = get_declared_classes();
if ($directory !== null) {
foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
require_once $file->getPathName();
}
}
}
$declared = get_declared_classes();
$tables = array();
foreach($declared as $name)
{
$class = new ReflectionClass($name);
if ($class->isSubclassOf($parent) AND !$class->isAbstract()) {
$tables[$name] = $name;
}
}
return $tables;
}
}
/** /**
* buildSchema * buildSchema
...@@ -69,8 +111,55 @@ abstract class Doctrine_Export_Schema ...@@ -69,8 +111,55 @@ abstract class Doctrine_Export_Schema
*/ */
public function buildSchema($directory) public function buildSchema($directory)
{ {
// we need to figure out how we can build all the model information for the passed directory/directories $array = array('tables' => array());
return array();
$tables = $this->getDirectoryTables($directory);
$parent = new ReflectionClass('Doctrine_Record');
$sql = array();
$fks = array();
// we iterate trhough the diff of previously declared classes
// and currently declared classes
foreach ($tables as $name) {
$class = new ReflectionClass($name);
$conn = Doctrine_Manager::getInstance()->getConnectionForComponent($name);
// check if class is an instance of Doctrine_Record and not abstract
// class must have method setTableDefinition (to avoid non-Record subclasses like symfony's sfDoctrineRecord)
// we have to recursively iterate through the class parents just to be sure that the classes using for example
// column aggregation inheritance are properly exported to database
while ($class->isAbstract() ||
! $class->isSubclassOf($parent) ||
! $class->hasMethod('setTableDefinition') ||
( $class->hasMethod('setTableDefinition') &&
$class->getMethod('setTableDefinition')->getDeclaringClass()->getName() !== $class->getName())) {
$class = $class->getParentClass();
if ($class === false) {
break;
}
}
if ($class === false) {
continue;
}
$record = new $name();
$table = $record->getTable();
$data = $table->getExportableFormat();
$table = array();
$table['name'] = $data['tableName'];
$table['class'] = get_class($record);
$table['columns'] = $data['columns'];
$array['tables'][$data['tableName']] = $table;
}
return $array;
} }
/** /**
...@@ -84,6 +173,6 @@ abstract class Doctrine_Export_Schema ...@@ -84,6 +173,6 @@ abstract class Doctrine_Export_Schema
{ {
$array = $this->buildSchema($directory); $array = $this->buildSchema($directory);
$this->dump($arr, $schema); return $this->dump($array, $schema);
} }
} }
\ No newline at end of file
...@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema ...@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema
*/ */
public function build($array) public function build($array)
{ {
return Doctrime_Parser::dump($array, null, 'xml'); return Doctrine_Parser::dumpXml($array, null);
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $array
* @param string $schema
* @return void
*/
public function dump($array, $schema)
{
$xml = $this->build($array);
file_put_contents($schema, $xml);
} }
} }
\ No newline at end of file
...@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema ...@@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema
*/ */
public function build($array) public function build($array)
{ {
return Doctrime_Parser::dump($array, null, 'yml'); return Doctrine_Parser::dumpYml($array, null);
}
/**
* dump
*
* Dump the array to the schema file
*
* @param string $arr
* @param string $schema
* @return void
*/
public function dump($arr, $schema)
{
$yml = $this->build($array);
file_put_contents($schema, $yml);
} }
} }
\ No newline at end of file
...@@ -52,26 +52,25 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema ...@@ -52,26 +52,25 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{ {
$xmlObj = $this->parse($schema); $xmlObj = $this->parse($schema);
// Go through all tables... foreach ($xmlObj->tables->table as $table) {
foreach ($xmlObj->table as $table) {
$columns = array(); $columns = array();
// Go through all columns... // Go through all columns...
foreach ($table->declaration->field as $field) { foreach ($table->columns as $column) {
$colDesc = array( $colDesc = array(
'name' => (string) $field->name, 'name' => (string) $column->name,
'type' => (string) $field->type, 'type' => (string) $column->type,
'ptype' => (string) $field->type, 'ptype' => (string) $column->type,
'length' => (int) $field->length, 'length' => (int) $column->length,
'fixed' => (int) $field->fixed, 'fixed' => (int) $column->fixed,
'unsigned' => (bool) $field->unsigned, 'unsigned' => (bool) $column->unsigned,
'primary' => (bool) (isset($field->primary) && $field->primary), 'primary' => (bool) (isset($column->primary) && $column->primary),
'default' => (string) $field->default, 'default' => (string) $column->default,
'notnull' => (bool) (isset($field->notnull) && $field->notnull), 'notnull' => (bool) (isset($column->notnull) && $column->notnull),
'autoinc' => (bool) (isset($field->autoincrement) && $field->autoincrement), 'autoinc' => (bool) (isset($column->autoincrement) && $column->autoincrement),
); );
$columns[(string) $field->name] = $colDesc; $columns[(string) $column->name] = $colDesc;
} }
$class = $table->class ? (string) $table->class:(string) $table->name; $class = $table->class ? (string) $table->class:(string) $table->name;
......
...@@ -51,12 +51,12 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema ...@@ -51,12 +51,12 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
public function parseSchema($schema) public function parseSchema($schema)
{ {
$array = $this->parse($schema); $array = $this->parse($schema);
$tables = $array['schema']['tables'];
// Go through all tables... foreach ($tables as $table) {
foreach ($array['tables'] as $table) {
$columns = array(); $columns = array();
foreach ($table['declaration'] as $field) { foreach ($table['columns'] as $field) {
$colDesc = array( $colDesc = array(
'name' => isset($field['name']) ? (string) $field['name']:null, 'name' => isset($field['name']) ? (string) $field['name']:null,
'type' => isset($field['type']) ? (string) $field['type']:null, 'type' => isset($field['type']) ? (string) $field['type']:null,
......
...@@ -31,26 +31,27 @@ ...@@ -31,26 +31,27 @@
*/ */
class Doctrine_Parser_Xml extends Doctrine_Parser class Doctrine_Parser_Xml extends Doctrine_Parser
{ {
public function arrayToXml($array) public function arrayToXml($data, $rootNodeName = 'data', $xml = null)
{ {
$this->text = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"; if ($xml === null) {
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$rootNodeName/>");
$this->text .= $this->arrayTransform($array); }
return $this->text; foreach($data as $key => $value)
} {
if (is_array($value)) {
$node = $xml->addChild($key);
$this->arrayToXml($value, $rootNodeName, $node);
} else {
$value = htmlentities($value);
$xml->addChild($key, $value);
}
public function arrayTransform($array) }
{
foreach ($array as $key => $value) { return $xml->asXML();
if (!is_array($value)) {
$this->text .= "<$key>$value</$key>";
} else {
$this->text.="<$key>";
$this->arrayTransform($value);
$this->text.="</$key>";
}
}
} }
public function dumpData($array, $path = null) public function dumpData($array, $path = null)
......
<?php <?php
define('LOAD_MODELS', false);
require_once('playground.php'); require_once('playground.php');
\ No newline at end of file
...@@ -6,15 +6,17 @@ require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'doctrine/Doctrine.php'; ...@@ -6,15 +6,17 @@ require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'doctrine/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'autoload'));
$modelsPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'models'; if (constant('LOAD_MODELS')) {
$modelsPath = dirname(__FILE__).DIRECTORY_SEPARATOR.'models';
// include the models
$models = new DirectoryIterator($modelsPath); // include the models
foreach($models as $key => $file) { $models = new DirectoryIterator($modelsPath);
if ($file->isFile() && ! $file->isDot()) { foreach($models as $key => $file) {
$e = explode('.', $file->getFileName()); if ($file->isFile() && ! $file->isDot()) {
if (end($e) === 'php') { $e = explode('.', $file->getFileName());
require_once $file->getPathname(); if (end($e) === 'php') {
require_once $file->getPathname();
}
} }
} }
} }
...@@ -25,26 +27,28 @@ $dbh = new PDO('sqlite::memory:'); ...@@ -25,26 +27,28 @@ $dbh = new PDO('sqlite::memory:');
$conn = Doctrine_Manager::connection($dbh); $conn = Doctrine_Manager::connection($dbh);
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL); if (constant('LOAD_MODELS')) {
$manager->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_ALL);
$tables = array('entity',
'entityReference', $tables = array('entity',
'email', 'entityReference',
'phonenumber', 'email',
'groupuser', 'phonenumber',
'album', 'groupuser',
'song', 'album',
'element', 'song',
'error', 'element',
'description', 'error',
'address', 'description',
'account', 'address',
'task', 'account',
'resource', 'task',
'assignment', 'resource',
'resourceType', 'assignment',
'resourceReference'); 'resourceType',
'resourceReference');
$conn->export->exportClasses($tables);
$conn->export->exportClasses($tables);
require_once('data.php');
\ No newline at end of file require_once('data.php');
}
\ No newline at end of file
<?xml version="1.0" encoding="ISO-8859-1" ?> <?xml version="1.0" encoding="ISO-8859-1" ?>
<tables> <schema>
<table> <tables>
<name>user</name> <table>
<class>User</class> <name>user</name>
<declaration> <class>User</class>
<field> <columns>
<name>id</name> <column>
<type>integer</type> <name>id</name>
<notnull>true</notnull> <type>integer</type>
<autoincrement>true</autoincrement> <notnull>true</notnull>
</field> <autoincrement>true</autoincrement>
<field> </column>
<name>username</name> <column>
<type>string</type> <name>username</name>
<length>20</length> <type>string</type>
<notnull>true</notnull> <length>20</length>
</field> <notnull>true</notnull>
</declaration> </column>
</table> </columns>
</table>
<table> <table>
<name>group</name> <name>group</name>
<class>Group</class> <class>Group</class>
<declaration> <columns>
<field> <column>
<name>id</name> <name>id</name>
<type>integer</type> <type>integer</type>
<notnull>true</notnull> <notnull>true</notnull>
<autoincrement>true</autoincrement> <autoincrement>true</autoincrement>
</field> </column>
<field> <column>
<name>name</name> <name>name</name>
<type>string</type> <type>string</type>
<length>20</length> <length>20</length>
<notnull>true</notnull> <notnull>true</notnull>
</field> </column>
</declaration> </columns>
</table> </table>
</tables> </tables>
\ No newline at end of file </schema>
\ No newline at end of file
--- ---
tables: schema:
user: tables:
name: user user:
class: User name: user
declaration: class: User
id: columns:
name: id id:
type: integer name: id
notnull: true type: integer
autoincrement: true notnull: true
username: autoincrement: true
name: username username:
type: string name: username
length: 20 type: string
notnull: true length: 20
group: notnull: true
name: group group:
class: Group name: group
declaration: class: Group
id: columns:
name: id id:
type: integer name: id
notnull: true type: integer
autoincrement: true notnull: true
name: autoincrement: true
name: name name:
type: string name: name
length: 20 type: string
notnull: true length: 20
\ No newline at end of file notnull: true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment