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

Getting exporting/importing working.

This commit is contained in:
Jonathan.Wage 2007-09-13 21:32:40 +00:00
parent 13d781cb68
commit c9658cb29b
10 changed files with 234 additions and 168 deletions

View File

@ -57,7 +57,49 @@ abstract class Doctrine_Export_Schema
* @param string $schema
* @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
@ -69,8 +111,55 @@ abstract class Doctrine_Export_Schema
*/
public function buildSchema($directory)
{
// we need to figure out how we can build all the model information for the passed directory/directories
return array();
$array = array('tables' => 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
{
$array = $this->buildSchema($directory);
$this->dump($arr, $schema);
return $this->dump($array, $schema);
}
}

View File

@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Xml extends Doctrine_Export_Schema
*/
public function build($array)
{
return Doctrime_Parser::dump($array, null, 'xml');
}
/**
* 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);
return Doctrine_Parser::dumpXml($array, null);
}
}

View File

@ -41,22 +41,6 @@ class Doctrine_Export_Schema_Yml extends Doctrine_Export_Schema
*/
public function build($array)
{
return Doctrime_Parser::dump($array, null, 'yml');
}
/**
* 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);
return Doctrine_Parser::dumpYml($array, null);
}
}

View File

@ -52,26 +52,25 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{
$xmlObj = $this->parse($schema);
// Go through all tables...
foreach ($xmlObj->table as $table) {
foreach ($xmlObj->tables->table as $table) {
$columns = array();
// Go through all columns...
foreach ($table->declaration->field as $field) {
foreach ($table->columns as $column) {
$colDesc = array(
'name' => (string) $field->name,
'type' => (string) $field->type,
'ptype' => (string) $field->type,
'length' => (int) $field->length,
'fixed' => (int) $field->fixed,
'unsigned' => (bool) $field->unsigned,
'primary' => (bool) (isset($field->primary) && $field->primary),
'default' => (string) $field->default,
'notnull' => (bool) (isset($field->notnull) && $field->notnull),
'autoinc' => (bool) (isset($field->autoincrement) && $field->autoincrement),
'name' => (string) $column->name,
'type' => (string) $column->type,
'ptype' => (string) $column->type,
'length' => (int) $column->length,
'fixed' => (int) $column->fixed,
'unsigned' => (bool) $column->unsigned,
'primary' => (bool) (isset($column->primary) && $column->primary),
'default' => (string) $column->default,
'notnull' => (bool) (isset($column->notnull) && $column->notnull),
'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;

View File

@ -51,12 +51,12 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
public function parseSchema($schema)
{
$array = $this->parse($schema);
$tables = $array['schema']['tables'];
// Go through all tables...
foreach ($array['tables'] as $table) {
foreach ($tables as $table) {
$columns = array();
foreach ($table['declaration'] as $field) {
foreach ($table['columns'] as $field) {
$colDesc = array(
'name' => isset($field['name']) ? (string) $field['name']:null,
'type' => isset($field['type']) ? (string) $field['type']:null,

View File

@ -31,26 +31,27 @@
*/
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\"?>";
$this->text .= $this->arrayTransform($array);
return $this->text;
}
if ($xml === null) {
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><$rootNodeName/>");
}
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) {
if (!is_array($value)) {
$this->text .= "<$key>$value</$key>";
} else {
$this->text.="<$key>";
$this->arrayTransform($value);
$this->text.="</$key>";
}
}
}
return $xml->asXML();
}
public function dumpData($array, $path = null)

View File

@ -1,2 +1,4 @@
<?php
define('LOAD_MODELS', false);
require_once('playground.php');

View File

@ -6,15 +6,17 @@ require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'doctrine/Doctrine.php';
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);
foreach($models as $key => $file) {
if ($file->isFile() && ! $file->isDot()) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php') {
require_once $file->getPathname();
// include the models
$models = new DirectoryIterator($modelsPath);
foreach($models as $key => $file) {
if ($file->isFile() && ! $file->isDot()) {
$e = explode('.', $file->getFileName());
if (end($e) === 'php') {
require_once $file->getPathname();
}
}
}
}
@ -25,26 +27,28 @@ $dbh = new PDO('sqlite::memory:');
$conn = Doctrine_Manager::connection($dbh);
$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',
'email',
'phonenumber',
'groupuser',
'album',
'song',
'element',
'error',
'description',
'address',
'account',
'task',
'resource',
'assignment',
'resourceType',
'resourceReference');
$tables = array('entity',
'entityReference',
'email',
'phonenumber',
'groupuser',
'album',
'song',
'element',
'error',
'description',
'address',
'account',
'task',
'resource',
'assignment',
'resourceType',
'resourceReference');
$conn->export->exportClasses($tables);
$conn->export->exportClasses($tables);
require_once('data.php');
require_once('data.php');
}

View File

@ -1,40 +1,42 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<tables>
<table>
<name>user</name>
<class>User</class>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</field>
<field>
<name>username</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</field>
</declaration>
</table>
<schema>
<tables>
<table>
<name>user</name>
<class>User</class>
<columns>
<column>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</column>
<column>
<name>username</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</column>
</columns>
</table>
<table>
<name>group</name>
<class>Group</class>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</field>
<field>
<name>name</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</field>
</declaration>
</table>
</tables>
<table>
<name>group</name>
<class>Group</class>
<columns>
<column>
<name>id</name>
<type>integer</type>
<notnull>true</notnull>
<autoincrement>true</autoincrement>
</column>
<column>
<name>name</name>
<type>string</type>
<length>20</length>
<notnull>true</notnull>
</column>
</columns>
</table>
</tables>
</schema>

View File

@ -1,30 +1,31 @@
---
tables:
user:
name: user
class: User
declaration:
id:
name: id
type: integer
notnull: true
autoincrement: true
username:
name: username
type: string
length: 20
notnull: true
group:
name: group
class: Group
declaration:
id:
name: id
type: integer
notnull: true
autoincrement: true
name:
name: name
type: string
length: 20
notnull: true
schema:
tables:
user:
name: user
class: User
columns:
id:
name: id
type: integer
notnull: true
autoincrement: true
username:
name: username
type: string
length: 20
notnull: true
group:
name: group
class: Group
columns:
id:
name: id
type: integer
notnull: true
autoincrement: true
name:
name: name
type: string
length: 20
notnull: true