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

Fixes to allow inheritance.

This commit is contained in:
Jonathan.Wage 2007-09-21 20:30:10 +00:00
parent 57ce06332f
commit d0898c1a81
4 changed files with 99 additions and 52 deletions

View File

@ -90,11 +90,10 @@ class Doctrine_Import_Builder
/** /**
* This class has been auto-generated by the Doctrine ORM Framework * This class has been auto-generated by the Doctrine ORM Framework
*/ */
class %s extends Doctrine_Record class %s extends %s
{ {
public function setTableDefinition() public function setTableDefinition()
{ {
%s
%s %s
} }
public function setUp() public function setUp()
@ -112,17 +111,31 @@ END;
* @param string $table * @param string $table
* @param array $tableColumns * @param array $tableColumns
*/ */
public function buildColumnDefinition(array $tableColumns) public function buildTableDefinition(array $options, array $columns, array $relations)
{ {
$columns = array(); $ret = array();
$i = 1;
$i = 0;
foreach ($tableColumns as $name => $column) {
$columns[$i] = ' $this->hasColumn(\'' . $name . '\', \'' . $column['type'] . '\''; 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']) { if ($column['length']) {
$columns[$i] .= ', ' . $column['length']; $ret[$i] .= ', ' . $column['length'];
} else { } else {
$columns[$i] .= ', null'; $ret[$i] .= ', null';
} }
$a = array(); $a = array();
@ -150,24 +163,32 @@ END;
} }
if ( ! empty($a)) { if ( ! empty($a)) {
$columns[$i] .= ', ' . 'array('; $ret[$i] .= ', ' . 'array(';
$length = strlen($columns[$i]); $length = strlen($ret[$i]);
$columns[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')';
} }
$columns[$i] .= ');';
$ret[$i] .= ');';
if ($i < (count($tableColumns) - 1)) { if ($i < (count($columns) - 1)) {
$columns[$i] .= PHP_EOL; $ret[$i] .= PHP_EOL;
} }
$i++; $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(); $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) { 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'] : '';
@ -219,27 +240,32 @@ END;
$i++; $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); return implode("\n", $ret);
} }
public function buildDefinition(array $options, array $columns, array $relations = array()) public function buildDefinition(array $options, array $columns, array $relations = array())
{ {
if ( ! isset($options['className'])) { if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
} }
$opt = isset($options['tableName']) && !empty($options['tableName']) ? str_repeat(' ', 8) . '$this->setTableName(\''. $options['tableName'].'\');':''; $className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
$content = sprintf(self::$tpl, $options['className'],$opt,
$this->buildColumnDefinition($columns), $content = sprintf(self::$tpl, $className,
$this->buildRelationDefinition($relations)); $extends,
$this->buildTableDefinition($options, $columns, $relations),
$this->buildSetUp($options, $columns, $relations));
return $content; return $content;
} }
public function buildRecord($options, $columns, $relations) public function buildRecord(array $options, array $columns, array $relations = array())
{ {
if ( ! isset($options['className'])) { if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');

View File

@ -73,8 +73,13 @@ class Doctrine_Import_Schema
$options = array(); $options = array();
$options['className'] = $properties['className']; $options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php'; $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$options['tableName'] = isset($properties['tableName'])?$properties['tableName']:null; $options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
$columns = $properties['columns'];
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(); $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; $className = isset($table['className']) ? (string) $table['className']:(string) $className;
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(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; $build[$className]['className'] = $className;
if (isset($table['columns'])) {
foreach ($table['columns'] as $columnName => $field) {
$build[$className]['columns'] = $columns; $colDesc = array();
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']: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]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
}
if (isset($table['inheritance'])) {
$build[$className]['inheritance'] = $table['inheritance'];
}
} }
return $build; return $build;
@ -134,9 +145,12 @@ class Doctrine_Import_Schema
public function buildRelationships($array) public function buildRelationships($array)
{ {
foreach ($array as $name => $properties) { foreach ($array as $name => $properties) {
if (!isset($properties['relations'])) {
continue;
}
$className = $properties['className']; $className = $properties['className'];
$relations = $properties['relations']; $relations = $properties['relations'];
$columns = $properties['columns'];
foreach ($relations as $alias => $relation) { foreach ($relations as $alias => $relation) {

View File

@ -2,4 +2,9 @@
require_once('playground.php'); require_once('playground.php');
require_once('connection.php'); require_once('connection.php');
require_once('models.php'); require_once('models.php');
require_once('data.php'); require_once('data.php');
$tables['Test'] = 'Test';
$import = new Doctrine_Import_Schema();
$import->importSchema('../tests/schema.yml', 'yml', 'test_models', $tables);

View File

@ -1,4 +1,6 @@
--- ---
Test:
inheritance: { extends: User, keyField: otype, keyValue: 1 }
Account: Account:
tableName: account tableName: account
className: Account className: Account