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

Fixes for yml importing/exporting of schema.

parent d62500e7
...@@ -167,6 +167,7 @@ END; ...@@ -167,6 +167,7 @@ END;
{ {
$ret = array(); $ret = array();
$i = 0; $i = 0;
foreach ($relations as $name => $relation) { foreach ($relations as $name => $relation) {
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : ''; $alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
...@@ -180,31 +181,43 @@ END; ...@@ -180,31 +181,43 @@ END;
} else { } else {
$ret[$i] = ' $this->hasMany(\'' . $name . $alias . '\''; $ret[$i] = ' $this->hasMany(\'' . $name . $alias . '\'';
} }
$a = array(); $a = array();
if (isset($relation['refClass'])) {
$a[] = '\'refClass\' => ' . var_export($relation['refClass'], true);
}
if (isset($relation['deferred']) && $relation['deferred']) { if (isset($relation['deferred']) && $relation['deferred']) {
$a[] = '\'default\' => ' . var_export($relation['deferred'], true); $a[] = '\'default\' => ' . var_export($relation['deferred'], true);
} }
if (isset($relation['local']) && $relation['local']) { if (isset($relation['local']) && $relation['local']) {
$a[] = '\'local\' => ' . var_export($relation['local'], true); $a[] = '\'local\' => ' . var_export($relation['local'], true);
} }
if (isset($relation['foreign']) && $relation['foreign']) { if (isset($relation['foreign']) && $relation['foreign']) {
$a[] = '\'foreign\' => ' . var_export($relation['foreign'], true); $a[] = '\'foreign\' => ' . var_export($relation['foreign'], true);
} }
if (isset($relation['onDelete']) && $relation['onDelete']) { if (isset($relation['onDelete']) && $relation['onDelete']) {
$a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true); $a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true);
} }
if (isset($relation['onUpdate']) && $relation['onUpdate']) { if (isset($relation['onUpdate']) && $relation['onUpdate']) {
$a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true); $a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true);
} }
if ( ! empty($a)) { if ( ! empty($a)) {
$ret[$i] .= ', ' . 'array('; $ret[$i] .= ', ' . 'array(';
$length = strlen($ret[$i]); $length = strlen($ret[$i]);
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
} }
$ret[$i] .= ');'; $ret[$i] .= ');';
$i++; $i++;
} }
return implode("\n", $ret); return implode("\n", $ret);
} }
......
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
*/ */
abstract class Doctrine_Import_Schema abstract class Doctrine_Import_Schema
{ {
public $relationColumns = array();
/** /**
* Parse the schema and return it in an array * Parse the schema and return it in an array
* *
...@@ -79,15 +81,54 @@ abstract class Doctrine_Import_Schema ...@@ -79,15 +81,54 @@ abstract class Doctrine_Import_Schema
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory); $builder->setTargetPath($directory);
$array = $this->parseSchema($schema); $array = array();
foreach ((array) $schema AS $s) {
$array = array_merge($array, $this->parseSchema($s));
}
$this->buildRelations($array);
foreach ($array as $name => $properties) { foreach ($array as $name => $properties) {
$options['className'] = $properties['class']; $options = array();
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['class'].'.class.php'; $options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$columns = $properties['columns'];
$relations = isset($this->relations[$options['className']]) ? $this->relations[$options['className']]:array();
$builder->buildRecord($options, $columns, $relations);
}
}
public function buildRelations($array)
{
foreach($array AS $name => $properties) {
$className = $properties['className'];
$columns = $properties['columns']; $columns = $properties['columns'];
$builder->buildRecord($options, $columns, array()); foreach ($columns as $column) {
if ($this->isRelation($column)) {
$this->addRelationColumn($className, $column);
}
}
} }
$this->processRelationships();
}
public function isRelation($column)
{
return isset($column['foreignClass']) && isset($column['foreignReference']);
}
public function addRelationColumn($className, $column)
{
$this->relationColumns[$className][] = $column;
}
public function processRelationships()
{
} }
} }
\ No newline at end of file
...@@ -52,7 +52,7 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema ...@@ -52,7 +52,7 @@ class Doctrine_Import_Schema_Xml extends Doctrine_Import_Schema
{ {
$xmlObj = $this->parse($schema); $xmlObj = $this->parse($schema);
foreach ($xmlObj->tables->table as $table) { foreach ($xmlObj->tables as $table) {
$columns = array(); $columns = array();
// Go through all columns... // Go through all columns...
......
...@@ -51,34 +51,40 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema ...@@ -51,34 +51,40 @@ 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']; foreach ($array as $tableName => $table) {
foreach ($tables as $table) {
$columns = array(); $columns = array();
foreach ($table['columns'] as $field) { $tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $tableName;
$colDesc = array( $className = isset($table['className']) ? (string) $table['className']:(string) $tableName;
'name' => isset($field['name']) ? (string) $field['name']:null,
'type' => isset($field['type']) ? (string) $field['type']:null,
'ptype' => isset($field['type']) ? (string) $field['type']:null,
'length' => isset($field['length']) ? (int) $field['length']:null,
'fixed' => isset($field['fixed']) ? (int) $field['fixed']:null,
'unsigned' => isset($field['unsigned']) ? (bool) $field['unsigned']:null,
'primary' => isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null,
'default' => isset($field['default']) ? (string) $field['default']:null,
'notnull' => isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null,
'autoinc' => isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null,
);
$columns[(string) $field['name']] = $colDesc; foreach ($table['columns'] as $columnName => $field) {
}
$class = $table['class'] ? (string) $table['class']:(string) $table['name']; $colDesc = array();
$colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName;
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
$colDesc['length'] = isset($field['length']) ? (int) $field['length']:null;
$colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null;
$colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
$colDesc['default'] = isset($field['default']) ? (string) $field['default']:null;
$colDesc['notnull'] = isset($field['notnull']) ? (bool) (isset($field['notnull']) && $field['notnull']):null;
$colDesc['autoinc'] = isset($field['autoinc']) ? (bool) (isset($field['autoinc']) && $field['autoinc']):null;
$colDesc['foreignClass'] = isset($field['foreignClass']) ? (string) $field['foreignClass']:null;
$colDesc['foreignReference'] = isset($field['foreignReference']) ? (string) $field['foreignReference']:null;
$colDesc['localName'] = isset($field['localName']) ? (string) $field['localName']:null;
$colDesc['foreignName'] = isset($field['foreignName']) ? (string) $field['foreignName']:null;
$colDesc['counterpart'] = isset($field['counterpart']) ? (string) $field['counterpart']:null;
$colDesc['onDelete'] = isset($field['onDelete']) ? (string) $field['onDelete']:null;
$colDesc['onUpdate'] = isset($field['onUpdate']) ? (string) $field['onUpdate']:null;
$columns[(string) $colDesc['name']] = $colDesc;
}
$tables[(string) $table['name']]['name'] = (string) $table['name']; $tables[$tableName]['tableName'] = $tableName;
$tables[(string) $table['name']]['class'] = (string) $class; $tables[$tableName]['className'] = $className;
$tables[(string) $table['name']]['columns'] = $columns; $tables[$tableName]['columns'] = $columns;
} }
return $tables; return $tables;
......
...@@ -304,11 +304,10 @@ $test->addTestCase(new Doctrine_RawSql_TestCase()); ...@@ -304,11 +304,10 @@ $test->addTestCase(new Doctrine_RawSql_TestCase());
$test->addTestCase(new Doctrine_NewCore_TestCase()); $test->addTestCase(new Doctrine_NewCore_TestCase());
//$test->addTestCase(new Doctrine_Import_Schema_Xml_TestCase());
//$test->addTestCase(new Doctrine_Export_Schema_Xml_TestCase());
$test->addTestCase(new Doctrine_Import_Schema_Yml_TestCase()); $test->addTestCase(new Doctrine_Import_Schema_Yml_TestCase());
$test->addTestCase(new Doctrine_Import_Schema_Xml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase()); $test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Xml_TestCase());
$test->addTestCase(new Doctrine_Template_TestCase()); $test->addTestCase(new Doctrine_Template_TestCase());
......
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