Fixes to allow inheritance.
This commit is contained in:
parent
57ce06332f
commit
d0898c1a81
@ -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;
|
||||
$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'] . '\'';
|
||||
|
||||
foreach ($tableColumns as $name => $column) {
|
||||
$columns[$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] .= ');';
|
||||
|
||||
if ($i < (count($tableColumns) - 1)) {
|
||||
$columns[$i] .= PHP_EOL;
|
||||
$ret[$i] .= ');';
|
||||
|
||||
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();
|
||||
|
||||
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,9 +240,13 @@ END;
|
||||
$i++;
|
||||
}
|
||||
|
||||
return implode("\n", $ret);
|
||||
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())
|
||||
{
|
||||
@ -229,17 +254,18 @@ END;
|
||||
throw new Doctrine_Import_Builder_Exception('Missing class name.');
|
||||
}
|
||||
|
||||
$className = $options['className'];
|
||||
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
|
||||
|
||||
$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));
|
||||
$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.');
|
||||
|
@ -74,7 +74,12 @@ class Doctrine_Import_Schema
|
||||
$options['className'] = $properties['className'];
|
||||
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
|
||||
$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();
|
||||
|
||||
@ -103,6 +108,9 @@ class Doctrine_Import_Schema
|
||||
$className = isset($table['className']) ? (string) $table['className']:(string) $className;
|
||||
$tableName = isset($table['tableName']) ? (string) $table['tableName']:(string) $className;
|
||||
|
||||
$build[$className]['className'] = $className;
|
||||
|
||||
if (isset($table['columns'])) {
|
||||
foreach ($table['columns'] as $columnName => $field) {
|
||||
|
||||
$colDesc = array();
|
||||
@ -122,21 +130,27 @@ class Doctrine_Import_Schema
|
||||
}
|
||||
|
||||
$build[$className]['tableName'] = $tableName;
|
||||
$build[$className]['className'] = $className;
|
||||
|
||||
$build[$className]['columns'] = $columns;
|
||||
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
|
||||
}
|
||||
|
||||
if (isset($table['inheritance'])) {
|
||||
$build[$className]['inheritance'] = $table['inheritance'];
|
||||
}
|
||||
}
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
|
@ -3,3 +3,8 @@ require_once('playground.php');
|
||||
require_once('connection.php');
|
||||
require_once('models.php');
|
||||
require_once('data.php');
|
||||
|
||||
$tables['Test'] = 'Test';
|
||||
|
||||
$import = new Doctrine_Import_Schema();
|
||||
$import->importSchema('../tests/schema.yml', 'yml', 'test_models', $tables);
|
@ -1,4 +1,6 @@
|
||||
---
|
||||
Test:
|
||||
inheritance: { extends: User, keyField: otype, keyValue: 1 }
|
||||
Account:
|
||||
tableName: account
|
||||
className: Account
|
||||
|
Loading…
Reference in New Issue
Block a user