diff --git a/lib/Doctrine.php b/lib/Doctrine.php index b00114aa9..c9bd77974 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -441,7 +441,7 @@ final class Doctrine */ public static function loadModels($directory) { - $declared = array(); + $declared = get_declared_classes(); if ($directory !== null) { foreach ((array) $directory as $dir) { @@ -456,7 +456,7 @@ final class Doctrine } } - $declared = get_declared_classes(); + $declared = array_diff(get_declared_classes(), $declared); } return self::getLoadedModels($declared); diff --git a/lib/Doctrine/Data/Export.php b/lib/Doctrine/Data/Export.php index 29ace20c1..1e84a16a0 100644 --- a/lib/Doctrine/Data/Export.php +++ b/lib/Doctrine/Data/Export.php @@ -156,7 +156,11 @@ class Doctrine_Data_Export extends Doctrine_Data $preparedData[$className][$recordKey][$relationClassName] = $relationValue; } else { // skip single primary keys, we need to maintain composite primary keys - $keys = $record->getTable()->getPrimaryKeys(); + $keys = $record->getTable()->getIdentifier(); + + if (!is_array($keys)) { + $keys = array($keys); + } if (count($keys) <= 1 && in_array($key, $keys)) { continue; diff --git a/lib/Doctrine/Data/Import.php b/lib/Doctrine/Data/Import.php index 528cdd0f7..694143674 100644 --- a/lib/Doctrine/Data/Import.php +++ b/lib/Doctrine/Data/Import.php @@ -173,7 +173,11 @@ class Doctrine_Data_Import extends Doctrine_Data $lorem = explode(' ', "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."); $columns = array_keys($record->toArray()); - $pks = $record->getTable()->getPrimaryKeys(); + $pks = $record->getTable()->getIdentifier(); + + if (!is_array($pks)) { + $pks = array($pks); + } foreach ($columns as $column) { diff --git a/lib/Doctrine/Import/Builder.php b/lib/Doctrine/Import/Builder.php index c519a75c0..9555d31d1 100644 --- a/lib/Doctrine/Import/Builder.php +++ b/lib/Doctrine/Import/Builder.php @@ -42,9 +42,13 @@ class Doctrine_Import_Builder private $path = ''; private $suffix = '.php'; - + + private $generateBaseClasses = false; + + private $baseClassesDirectory = 'generated'; + private static $tpl; - + public function __construct() { $this->loadTemplate(); @@ -64,6 +68,25 @@ class Doctrine_Import_Builder $this->path = $path; } + + /** + * generateBaseClasses + * + * Specify whether or not to generate classes which extend from generated base classes + * + * @param string $bool + * @return void + * @author Jonathan H. Wage + */ + public function generateBaseClasses($bool = null) + { + if ($bool !== null) { + $this->generateBaseClasses = $bool; + } + + return $this->generateBaseClasses; + } + /** * getTargetPath * @@ -92,14 +115,8 @@ class Doctrine_Import_Builder */ class %s extends %s { - public function setTableDefinition() - { %s - } - public function setUp() - { %s - } } END; @@ -117,6 +134,9 @@ END; $i = 0; + $ret[$i] = "\t\tpublic function setTableDefinition()\n\t\t{"; + $i++; + if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) { $ret[$i] = "\t\tparent::setTableDefinition();"; @@ -149,7 +169,7 @@ END; if (isset($column['primary']) && $column['primary']) { $a[] = '\'primary\' => true'; } - if (isset($column['autoinc']) && $column['autoinc']) { + if ((isset($column['autoinc']) && $column['autoinc']) || isset($column['autoincrement']) && $column['autoincrement']) { $a[] = '\'autoincrement\' => true'; } if (isset($column['unique']) && $column['unique']) { @@ -176,18 +196,20 @@ END; $i++; } - return implode("\n", $ret); + return implode("\n", $ret)."\n\t\t}"; } 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 = 0; - $i = 1; - } else { - $i = 0; + $ret[$i] = "\t\tpublic function setUp()\n\t\t{"; + $i++; + + if (isset($options['inheritance']) && isset($options['inheritance']['extends'])) { + $ret[$i] = "\t\tparent::setUp();"; + $i++; } foreach ($relations as $name => $relation) { @@ -245,7 +267,7 @@ END; $ret[$i] = "\t\t".'$this->setInheritanceMap(array(\''.$options['inheritance']['keyField'].'\' => '.$options['inheritance']['keyValue'].'));'; } - return implode("\n", $ret); + return implode("\n", $ret)."\n\t\t}"; } public function buildDefinition(array $options, array $columns, array $relations = array()) @@ -257,41 +279,74 @@ END; $className = $options['className']; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; + if (!isset($options['inheritance']['extends'])) { + $definition = $this->buildTableDefinition($options, $columns, $relations); + $setUp = $this->buildSetUp($options, $columns, $relations); + } else { + $definition = null; + $setUp = null; + } + $content = sprintf(self::$tpl, $className, $extends, - $this->buildTableDefinition($options, $columns, $relations), - $this->buildSetUp($options, $columns, $relations)); - + $definition, + $setUp); + return $content; } 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.'); } - if ( ! isset($options['fileName'])) { + if ( !isset($options['fileName'])) { if (empty($this->path)) { - $errMsg = 'No build target directory set.'; - throw new Doctrine_Import_Builder_Exception($errMsg); + throw new Doctrine_Import_Builder_Exception('No build target directory set.'); } if (is_writable($this->path) === false) { - $errMsg = 'Build target directory ' . $this->path . ' is not writable.'; - throw new Doctrine_Import_Builder_Exception($errMsg); + throw new Doctrine_Import_Builder_Exception('Build target directory ' . $this->path . ' is not writable.'); } $options['fileName'] = $this->path . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; } - - $content = $this->buildDefinition($options, $columns, $relations); - - $bytes = file_put_contents($options['fileName'], 'generateBaseClasses()) { + + $optionsBak = $options; + + unset($options['tableName']); + $options['inheritance']['extends'] = 'Base' . $options['className']; + $this->writeDefinition($options, array(), array()); + + $options = $optionsBak; + + $generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory; + + if (!file_exists($generatedPath)) { + mkdir($generatedPath); + } + + $options['className'] = 'Base' . $options['className']; + $options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; + + $this->writeDefinition($options, $columns, $relations); + } else { + $this->writeDefinition($options, $columns, $relations); } } + + public function writeDefinition(array $options, array $columns, array $relations = array()) + { + $content = $this->buildDefinition($options, $columns, $relations); + + $bytes = file_put_contents($options['fileName'], 'setTargetPath($directory); + $builder->generateBaseClasses(true); $schema = $this->buildSchema($schema, $format); @@ -77,23 +78,38 @@ class Doctrine_Import_Schema continue; } - $options = array(); - $options['className'] = $properties['className']; - $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php'; - $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(); + $options = $this->getOptions($properties, $directory); + $columns = $this->getColumns($properties); + $relations = $this->getRelations($properties); $builder->buildRecord($options, $columns, $relations); } } + public function getOptions($properties, $directory) + { + $options = array(); + $options['className'] = $properties['className']; + $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php'; + $options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null; + + if (isset($properties['inheritance'])) { + $options['inheritance'] = $properties['inheritance']; + } + + return $options; + } + + public function getColumns($properties) + { + return isset($properties['columns']) ? $properties['columns']:array(); + } + + public function getRelations($properties) + { + return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array(); + } + /** * parseSchema * @@ -130,7 +146,7 @@ class Doctrine_Import_Schema $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['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null; $colDesc['values'] = isset($field['values']) ? (array) $field['values']: null; $columns[(string) $colDesc['name']] = $colDesc; diff --git a/lib/Doctrine/Parser/Yml.php b/lib/Doctrine/Parser/Yml.php index d071b3f36..690eb34c6 100644 --- a/lib/Doctrine/Parser/Yml.php +++ b/lib/Doctrine/Parser/Yml.php @@ -1,5 +1,5 @@