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

Fixes to allow inheritance.

parent 57ce0633
......@@ -90,11 +90,10 @@ class Doctrine_Import_Builder
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class %s extends Doctrine_Record
class %s extends %s
{
public function setTableDefinition()
{
%s
%s
}
public function setUp()
......@@ -112,17 +111,31 @@ END;
* @param string $table
* @param array $tableColumns
*/
public function buildColumnDefinition(array $tableColumns)
public function buildTableDefinition(array $options, array $columns, array $relations)
{
$columns = array();
$i = 1;
foreach ($tableColumns as $name => $column) {
$columns[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\'';
$ret = array();
$i = 0;
if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) {
$ret[$i] = "\t\tparent::setTableDefinition();";
$i++;
}
if (isset($options['tableName']) && !empty($options['tableName'])) {
$ret[$i] = str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');';
$i++;
}
foreach ($columns as $name => $column) {
$ret[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\'';
if ($column['length']) {
$columns[$i] .= ', ' . $column['length'];
$ret[$i] .= ', ' . $column['length'];
} else {
$columns[$i] .= ', null';
$ret[$i] .= ', null';
}
$a = array();
......@@ -150,24 +163,32 @@ END;
}
if ( ! empty($a)) {
$columns[$i] .= ', ' . 'array(';
$length = strlen($columns[$i]);
$columns[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
$ret[$i] .= ', ' . 'array(';
$length = strlen($ret[$i]);
$ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
}
$columns[$i] .= ');';
$ret[$i] .= ');';
if ($i < (count($tableColumns) - 1)) {
$columns[$i] .= PHP_EOL;
if ($i < (count($columns) - 1)) {
$ret[$i] .= PHP_EOL;
}
$i++;
}
return implode("\n", $columns);
return implode("\n", $ret);
}
public function buildRelationDefinition(array $relations)
public function buildSetUp(array $options, array $columns, array $relations)
{
$ret = array();
$i = 0;
if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) {
$ret[1] = "\t\tparent::setUp();";
$i = 1;
} else {
$i = 0;
}
foreach ($relations as $name => $relation) {
$alias = (isset($relation['alias']) && $relation['alias'] !== $name) ? ' as ' . $relation['alias'] : '';
......@@ -219,27 +240,32 @@ END;
$i++;
}
if (isset($options['inheritance']['keyField']) && isset($options['inheritance']['keyValue'])) {
$i++;
$ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));';
}
return implode("\n", $ret);
}
public function buildDefinition(array $options, array $columns, array $relations = array())
{
if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
}
$opt = isset($options['tableName']) && !empty($options['tableName']) ? str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');':'';
$content = sprintf(self::$tpl, $options['className'],$opt,
$this->buildColumnDefinition($columns),
$this->buildRelationDefinition($relations));
$className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
$content = sprintf(self::$tpl, $className,
$extends,
$this->buildTableDefinition($options, $columns, $relations),
$this->buildSetUp($options, $columns, $relations));
return $content;
}
public function buildRecord($options, $columns, $relations)
public function buildRecord(array $options, array $columns, array $relations = array())
{
if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......
......@@ -73,8 +73,13 @@ class Doctrine_Import_Schema
$options = array();
$options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$options['tableName'] = isset($properties['tableName'])?$properties['tableName']:null;
$columns = $properties['columns'];
$options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
if (isset($properties['inheritance'])) {
$options['inheritance'] = $properties['inheritance'];
}
$columns = isset($properties['columns']) ? $properties['columns']:array();
$relations = isset($this->relations[$options['className']]) ? $this->relations[$options['className']]:array();
......@@ -103,29 +108,35 @@ class Doctrine_Import_Schema
$className = isset($table['className']) ? (string) $table['className']:(string) $className;
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $className;
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['values'] = isset($field['values']) ? (array) $field['values']: null;
$columns[(string) $colDesc['name']] = $colDesc;
}
$build[$className]['tableName'] = $tableName;
$build[$className]['className'] = $className;
if (isset($table['columns'])) {
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['values'] = isset($field['values']) ? (array) $field['values']: null;
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$columns[(string) $colDesc['name']] = $colDesc;
}
$build[$className]['tableName'] = $tableName;
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
}
if (isset($table['inheritance'])) {
$build[$className]['inheritance'] = $table['inheritance'];
}
}
return $build;
......@@ -134,9 +145,12 @@ class Doctrine_Import_Schema
public function buildRelationships($array)
{
foreach ($array as $name => $properties) {
if (!isset($properties['relations'])) {
continue;
}
$className = $properties['className'];
$relations = $properties['relations'];
$columns = $properties['columns'];
foreach ($relations as $alias => $relation) {
......
......@@ -2,4 +2,9 @@
require_once('playground.php');
require_once('connection.php');
require_once('models.php');
require_once('data.php');
\ No newline at end of file
require_once('data.php');
$tables['Test'] = 'Test';
$import = new Doctrine_Import_Schema();
$import->importSchema('../tests/schema.yml', 'yml', 'test_models', $tables);
\ No newline at end of file
---
Test:
inheritance: { extends: User, keyField: otype, keyValue: 1 }
Account:
tableName: account
className: Account
......
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