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

Fixes for yml importing/exporting of schema.

parent d62500e7
......@@ -167,6 +167,7 @@ END;
{
$ret = array();
$i = 0;
foreach ($relations as $name => $relation) {
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
......@@ -180,31 +181,43 @@ END;
} else {
$ret[$i] = ' $this->hasMany(\'' . $name . $alias . '\'';
}
$a = array();
if (isset($relation['refClass'])) {
$a[] = '\'refClass\' => ' . var_export($relation['refClass'], true);
}
if (isset($relation['deferred']) && $relation['deferred']) {
$a[] = '\'default\' => ' . var_export($relation['deferred'], true);
}
if (isset($relation['local']) && $relation['local']) {
$a[] = '\'local\' => ' . var_export($relation['local'], true);
}
if (isset($relation['foreign']) && $relation['foreign']) {
$a[] = '\'foreign\' => ' . var_export($relation['foreign'], true);
}
if (isset($relation['onDelete']) && $relation['onDelete']) {
$a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true);
}
if (isset($relation['onUpdate']) && $relation['onUpdate']) {
$a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true);
}
if ( ! empty($a)) {
$ret[$i] .= ', ' . 'array(';
$length = strlen($ret[$i]);
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
}
$ret[$i] .= ');';
$i++;
}
return implode("\n", $ret);
}
......
......@@ -38,7 +38,9 @@
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
abstract class Doctrine_Import_Schema
{
{
public $relationColumns = array();
/**
* Parse the schema and return it in an array
*
......@@ -78,16 +80,55 @@ abstract class Doctrine_Import_Schema
{
$builder = new Doctrine_Import_Builder();
$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) {
$options['className'] = $properties['class'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['class'].'.class.php';
$options = array();
$options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$columns = $properties['columns'];
$builder->buildRecord($options, $columns, array());
$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'];
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
{
$xmlObj = $this->parse($schema);
foreach ($xmlObj->tables->table as $table) {
foreach ($xmlObj->tables as $table) {
$columns = array();
// Go through all columns...
......
......@@ -51,34 +51,40 @@ class Doctrine_Import_Schema_Yml extends Doctrine_Import_Schema
public function parseSchema($schema)
{
$array = $this->parse($schema);
$tables = $array['schema']['tables'];
foreach ($tables as $table) {
foreach ($array as $tableName => $table) {
$columns = array();
foreach ($table['columns'] as $field) {
$colDesc = array(
'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,
);
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $tableName;
$className = isset($table['className']) ? (string) $table['className']:(string) $tableName;
$columns[(string) $field['name']] = $colDesc;
foreach ($table['columns'] as $columnName => $field) {
$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;
}
$class = $table['class'] ? (string) $table['class']:(string) $table['name'];
$tables[(string) $table['name']]['name'] = (string) $table['name'];
$tables[(string) $table['name']]['class'] = (string) $class;
$tables[$tableName]['tableName'] = $tableName;
$tables[$tableName]['className'] = $className;
$tables[(string) $table['name']]['columns'] = $columns;
$tables[$tableName]['columns'] = $columns;
}
return $tables;
......
......@@ -304,11 +304,10 @@ $test->addTestCase(new Doctrine_RawSql_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_Xml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Yml_TestCase());
$test->addTestCase(new Doctrine_Export_Schema_Xml_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