Commit 0da62430 authored by Jonathan.Wage's avatar Jonathan.Wage

Fixes to model generation.

parent bb1e87b0
...@@ -38,13 +38,6 @@ ...@@ -38,13 +38,6 @@
*/ */
class Doctrine_Import_Builder class Doctrine_Import_Builder
{ {
/**
* written
*
* @var array
*/
private $written = array();
/** /**
* Path * Path
* *
...@@ -59,7 +52,7 @@ class Doctrine_Import_Builder ...@@ -59,7 +52,7 @@ class Doctrine_Import_Builder
* *
* @var string * @var string
*/ */
private $packagePrefix = 'Package'; private $packagesPrefix = 'Package';
/** /**
* packagesPath * packagesPath
...@@ -101,7 +94,14 @@ class Doctrine_Import_Builder ...@@ -101,7 +94,14 @@ class Doctrine_Import_Builder
* @var string $suffix * @var string $suffix
*/ */
private $baseClassesDirectory = 'generated'; private $baseClassesDirectory = 'generated';
/**
* baseClassName
*
* @var string
*/
private $baseClassName = 'Doctrine_Record';
/** /**
* tpl * tpl
* *
...@@ -141,12 +141,12 @@ class Doctrine_Import_Builder ...@@ -141,12 +141,12 @@ class Doctrine_Import_Builder
/** /**
* setPackagePath * setPackagePath
* *
* @param string $packagePrefix * @param string $packagesPrefix
* @return void * @return void
*/ */
public function setPackagePrefix($packagePrefix) public function setPackagesPrefix($packagesPrefix)
{ {
$this->packagePrefix = $packagePrefix; $this->packagesPrefix = $packagesPrefix;
} }
/** /**
...@@ -204,6 +204,16 @@ class Doctrine_Import_Builder ...@@ -204,6 +204,16 @@ class Doctrine_Import_Builder
$this->baseClassesDirectory; $this->baseClassesDirectory;
} }
/**
* setBaseClassName
*
* @package default
*/
public function setBaseClassName($className)
{
$this->baseClassName = $className;
}
/** /**
* setSuffix * setSuffix
* *
...@@ -289,7 +299,7 @@ END; ...@@ -289,7 +299,7 @@ END;
$i = 0; $i = 0;
if (isset($options['inheritance']['extends']) && !isset($options['override_parent'])) { if (isset($options['inheritance']['extends']) && !(isset($options['override_parent']) && $options['override_parent'] == false)) {
$ret[$i] = "\t\tparent::setTableDefinition();"; $ret[$i] = "\t\tparent::setTableDefinition();";
$i++; $i++;
} }
...@@ -358,8 +368,11 @@ END; ...@@ -358,8 +368,11 @@ END;
$ret[$i] = $this->buildActAs($actAs); $ret[$i] = $this->buildActAs($actAs);
if ( ! empty($ret)) { $code = implode("\n", $ret);
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}"; $code = trim($code);
if ($code) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n\t\t".$code."\n\t}";
} }
} }
...@@ -539,7 +552,7 @@ END; ...@@ -539,7 +552,7 @@ END;
$ret = array(); $ret = array();
$i = 0; $i = 0;
if ( ! (isset($options['override_parent']) && $options['override_parent'] === true)) { if (isset($options['inheritance']['extends']) && !(isset($options['override_parent']) && $options['override_parent'] == false)) {
$ret[$i] = "\t\tparent::setUp();"; $ret[$i] = "\t\tparent::setUp();";
$i++; $i++;
} }
...@@ -604,8 +617,11 @@ END; ...@@ -604,8 +617,11 @@ END;
$ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));'; $ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));';
} }
if ( ! empty($ret)) { $code = implode("\n", $ret);
return "\n\tpublic function setUp()\n\t{\n".implode("\n", $ret)."\n\t}"; $code = trim($code);
if ($code) {
return "\n\tpublic function setUp()\n\t{\n\t\t".$code."\n\t}";
} }
} }
...@@ -629,7 +645,7 @@ END; ...@@ -629,7 +645,7 @@ END;
$abstract = isset($options['abstract']) && $options['abstract'] === true ? 'abstract ':null; $abstract = isset($options['abstract']) && $options['abstract'] === true ? 'abstract ':null;
$className = $options['className']; $className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:$this->baseClassName;
if ( ! (isset($options['no_definition']) && $options['no_definition'] === true)) { if ( ! (isset($options['no_definition']) && $options['no_definition'] === true)) {
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs); $definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
...@@ -686,7 +702,7 @@ END; ...@@ -686,7 +702,7 @@ END;
// If we have a package then we need to make this extend the package definition and not the base definition // If we have a package then we need to make this extend the package definition and not the base definition
// The package definition will then extends the base definition // The package definition will then extends the base definition
$topLevel['inheritance']['extends'] = (isset($topLevel['package']) && $topLevel['package']) ? $this->packagePrefix . $topLevel['className']:'Base' . $topLevel['className']; $topLevel['inheritance']['extends'] = (isset($topLevel['package']) && $topLevel['package']) ? $this->packagesPrefix . $topLevel['className']:'Base' . $topLevel['className'];
$topLevel['no_definition'] = true; $topLevel['no_definition'] = true;
$topLevel['generate_once'] = true; $topLevel['generate_once'] = true;
$topLevel['is_main_class'] = true; $topLevel['is_main_class'] = true;
...@@ -744,8 +760,6 @@ END; ...@@ -744,8 +760,6 @@ END;
$writePath = $path . DIRECTORY_SEPARATOR . $className . $this->suffix; $writePath = $path . DIRECTORY_SEPARATOR . $className . $this->suffix;
$this->written[$className] = $writePath;
if (!file_exists($writePath)) { if (!file_exists($writePath)) {
file_put_contents($writePath, $content); file_put_contents($writePath, $content);
} }
...@@ -834,10 +848,7 @@ END; ...@@ -834,10 +848,7 @@ END;
} }
$code .= PHP_EOL . $definition; $code .= PHP_EOL . $definition;
$this->written[$options['className']] = $writePath;
if (isset($options['generate_once']) && $options['generate_once'] === true) { if (isset($options['generate_once']) && $options['generate_once'] === true) {
if (!file_exists($writePath)) { if (!file_exists($writePath)) {
$bytes = file_put_contents($writePath, $code); $bytes = file_put_contents($writePath, $code);
......
...@@ -39,26 +39,40 @@ ...@@ -39,26 +39,40 @@
*/ */
class Doctrine_Import_Schema class Doctrine_Import_Schema
{ {
public $relations = array(); protected $relations = array();
public $generateBaseClasses = false; protected $options = array('packagesPrefix' => 'Package',
'packagesPath' => '',
'generateBaseClasses' => true,
'generateTableClasses' => true,
'baseClassesDirectory' => 'generated',
'baseClassName' => 'Doctrine_Record',
'suffix' => '.class.php');
/** /**
* generateBaseClasses * getOption
* *
* Specify whether or not to generate base classes with the model definition in it. The base is generated everytime * @param string $name
* But another child class that extends the base is only generated once. Allowing you to customize your models * @return void
* Without losing the changes when you regenerate */
public function getOption($name)
{
if (isset($this->options[$name])) {
return $this->options[$name];
}
}
/**
* setOption
* *
* @param string $bool * @param string $name
* @return bool $generateBaseClasses * @param string $value
* @return void
*/ */
public function generateBaseClasses($bool = null) public function setOption($name, $value)
{ {
if ($bool !== null) { if (isset($this->options[$name])) {
$this->generateBaseClasses = $bool; $this->options[$name] = $value;
} }
return $this->generateBaseClasses;
} }
/** /**
...@@ -110,13 +124,23 @@ class Doctrine_Import_Schema ...@@ -110,13 +124,23 @@ class Doctrine_Import_Schema
{ {
$builder = new Doctrine_Import_Builder(); $builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory); $builder->setTargetPath($directory);
$builder->generateBaseClasses($this->generateBaseClasses()); $builder->generateBaseClasses($this->getOption('generateBaseClasses'));
$builder->generateTableClasses($this->getOption('generateTableClasses'));
$builder->setBaseClassesDirectory($this->getOption('baseClassesDirectory'));
$builder->setBaseClassName($this->getOption('baseClassName'));
$builder->setPackagesPath($this->getOption('packagesPath'));
$builder->setPackagesPrefix($this->getOption('packagesPrefix'));
$builder->setSuffix($this->getOption('suffix'));
$schema = $this->buildSchema($schema, $format); $schema = $this->buildSchema($schema, $format);
$array = $schema['schema']; $array = $schema['schema'];
foreach ($array as $name => $properties) { foreach ($array as $name => $properties) {
if (!isset($properties['className'])) {
print_r($properties);
exit;
}
if ( ! empty($models) && !in_array($properties['className'], $models)) { if ( ! empty($models) && !in_array($properties['className'], $models)) {
continue; continue;
} }
...@@ -253,7 +277,16 @@ class Doctrine_Import_Schema ...@@ -253,7 +277,16 @@ class Doctrine_Import_Schema
$columns = array(); $columns = array();
$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) Doctrine::tableize($className);
if (isset($table['tableName']) && $table['tableName']) {
$tableName = $table['tableName'];
} else {
if (isset($table['inheritance']['extends']) && isset($table['inheritance']['extends']['keyType']) && isset($table['inheritance']['extends']['keyValue'])) {
$tableName = null;
} else {
$tableName = Doctrine::tableize($className);
}
}
$columns = isset($table['columns']) ? $table['columns']:array(); $columns = isset($table['columns']) ? $table['columns']:array();
$columns = isset($table['fields']) ? $table['fields']:$columns; $columns = isset($table['fields']) ? $table['fields']:$columns;
...@@ -262,10 +295,19 @@ class Doctrine_Import_Schema ...@@ -262,10 +295,19 @@ class Doctrine_Import_Schema
foreach ($columns as $columnName => $field) { foreach ($columns as $columnName => $field) {
$colDesc = array(); $colDesc = array();
$colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName; $colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName;
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
$e = explode('(', $field['type']);
if (isset($e[0]) && isset($e[1])) {
$colDesc['type'] = $e[0];
$colDesc['length'] = substr($e[1], 0, strlen($e[1]) - 1);
} else {
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
$colDesc['length'] = isset($field['length']) ? (int) $field['length']:null;
$colDesc['length'] = isset($field['size']) ? (int) $field['size']:$colDesc['length'];
}
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type']; $colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
$colDesc['length'] = isset($field['length']) ? (int) $field['length']:null;
$colDesc['length'] = isset($field['size']) ? (int) $field['size']:$colDesc['length'];
$colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null; $colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null;
$colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null; $colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null; $colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
...@@ -278,19 +320,19 @@ class Doctrine_Import_Schema ...@@ -278,19 +320,19 @@ class Doctrine_Import_Schema
$columns[(string) $colDesc['name']] = $colDesc; $columns[(string) $colDesc['name']] = $colDesc;
} }
$build[$className]['connection'] = isset($table['connection']) ? $table['connection']:null;
$build[$className]['className'] = $className;
$build[$className]['tableName'] = $tableName;
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array();
$build[$className]['package'] = isset($table['package']) ? $table['package']:null;
} }
$build[$className]['connection'] = isset($table['connection']) ? $table['connection']:null;
$build[$className]['className'] = $className;
$build[$className]['tableName'] = $tableName;
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array();
$build[$className]['package'] = isset($table['package']) ? $table['package']:null;
if (isset($table['inheritance'])) { if (isset($table['inheritance'])) {
$build[$className]['inheritance'] = $table['inheritance']; $build[$className]['inheritance'] = $table['inheritance'];
} }
...@@ -347,33 +389,6 @@ class Doctrine_Import_Schema ...@@ -347,33 +389,6 @@ class Doctrine_Import_Schema
$relation['foreignType'] = $relation['foreignType'] === 'one' ? Doctrine_Relation::ONE:Doctrine_Relation::MANY; $relation['foreignType'] = $relation['foreignType'] === 'one' ? Doctrine_Relation::ONE:Doctrine_Relation::MANY;
} }
if(isset($relation['refClass']) && !empty($relation['refClass']) && ( ! isset($array[$relation['refClass']]['relations']) || empty($array[$relation['refClass']]['relations']))) {
if ( ! isset($array[$relation['refClass']]['relations'][$className]['local'])) {
$array[$relation['refClass']]['relations'][$className]['local'] = $relation['local'];
}
if ( ! isset($array[$relation['refClass']]['relations'][$className]['foreign'])) {
$array[$relation['refClass']]['relations'][$className]['foreign'] = $relation['foreign'];
}
$array[$relation['refClass']]['relations'][$className]['ignore'] = true;
if ( ! isset($array[$relation['refClass']]['relations'][$relation['class']]['local'])) {
$array[$relation['refClass']]['relations'][$relation['class']]['local'] = $relation['local'];
}
if ( ! isset($array[$relation['refClass']]['relations'][$relation['class']]['foreign'])) {
$array[$relation['refClass']]['relations'][$relation['class']]['foreign'] = $relation['foreign'];
}
$array[$relation['refClass']]['relations'][$relation['class']]['ignore'] = true;
if(isset($relation['foreignAlias'])) {
$array[$relation['class']]['relations'][$relation['foreignAlias']] = array('type'=>$relation['type'],'local'=>$relation['foreign'],'foreign'=>$relation['local'],'refClass'=>$relation['refClass'],'class'=>$className);
}
}
$this->relations[$className][$alias] = $relation; $this->relations[$className][$alias] = $relation;
} }
} }
...@@ -393,32 +408,26 @@ class Doctrine_Import_Schema ...@@ -393,32 +408,26 @@ class Doctrine_Import_Schema
{ {
foreach($this->relations as $className => $relations) { foreach($this->relations as $className => $relations) {
foreach ($relations AS $alias => $relation) { foreach ($relations AS $alias => $relation) {
if(isset($relation['ignore']) && $relation['ignore'] || isset($relation['refClass']) || isset($this->relations[$relation['class']]['relations'][$className])) {
continue;
}
$newRelation = array(); $newRelation = array();
$newRelation['foreign'] = $relation['local']; $newRelation['foreign'] = $relation['local'];
$newRelation['local'] = $relation['foreign']; $newRelation['local'] = $relation['foreign'];
$newRelation['class'] = $className; $newRelation['class'] = isset($relation['foreignClass']) ? $relation['foreignClass']:$className;
$newRelation['alias'] = isset($relation['foreignAlias'])?$relation['foreignAlias']:$className; $newRelation['alias'] = isset($relation['foreignAlias']) ? $relation['foreignAlias']:$className;
if(isset($relation['foreignType'])) { if (isset($relation['refClass'])) {
$newRelation['type'] = $relation['foreignType']; $newRelation['refClass'] = $relation['refClass'];
} else { $newRelation['type'] = isset($relation['foreignType']) ? $relation['foreignType']:$relation['type'];
$newRelation['type'] = $relation['type'] === Doctrine_Relation::ONE ? Doctrine_Relation::MANY:Doctrine_Relation::ONE; } else {
} if(isset($relation['foreignType'])) {
$newRelation['type'] = $relation['foreignType'];
if( isset($this->relations[$relation['class']]) && is_array($this->relations[$relation['class']]) ) { } else {
foreach($this->relations[$relation['class']] as $otherRelation) { $newRelation['type'] = $relation['type'] === Doctrine_Relation::ONE ? Doctrine_Relation::MANY:Doctrine_Relation::ONE;
// skip fully defined m2m relationships
if(isset($otherRelation['refClass']) && $otherRelation['refClass'] == $className) {
continue(2);
}
} }
} }
$this->relations[$relation['class']][$newRelation['alias']] = $newRelation; if (!isset($this->relations[$relation['class']][$newRelation['alias']])) {
$this->relations[$relation['class']][$newRelation['alias']] = $newRelation;
}
} }
} }
} }
......
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