diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 859a34a66..843395473 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -554,7 +554,7 @@ abstract class AbstractPlatform * * Example * array( - * 'fields' => array( + * 'columns' => array( * 'user_name' => array(), * 'last_login' => array() * ) @@ -571,11 +571,11 @@ abstract class AbstractPlatform $query .= ' UNIQUE'; } - $fields = array(); - foreach (array_keys($definition['fields']) as $field) { - $fields[] = $field; + $columns = array(); + foreach (array_keys($definition['columns']) as $column) { + $columns[] = $column; } - $query .= ' ('. implode(', ', $fields) . ')'; + $query .= ' ('. implode(', ', $columns) . ')'; return $query; } @@ -590,7 +590,7 @@ abstract class AbstractPlatform */ public function getCreateIndexSql($table, $name, array $definition) { - if ( ! isset($definition['fields'])) { + if ( ! isset($definition['columns'])) { throw DoctrineException::indexFieldsArrayRequired(); } @@ -607,7 +607,7 @@ abstract class AbstractPlatform $query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table; - $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['fields']) . ')'; + $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')'; return $query; } @@ -874,13 +874,13 @@ abstract class AbstractPlatform } } - if ( ! isset($definition['fields']) || ! is_array($definition['fields'])) { + if ( ! isset($definition['columns']) || ! is_array($definition['columns'])) { throw DoctrineException::indexFieldsArrayRequired(); } $query = $type . 'INDEX ' . $name; - $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['fields']) . ')'; + $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')'; return $query; } diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index e1548a520..1895e7bd1 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -421,34 +421,6 @@ class MySqlPlatform extends AbstractPlatform } $queryFields = $this->getColumnDeclarationListSql($fields); - // build indexes for all foreign key fields (needed in MySQL!!) - /*if (isset($options['foreignKeys'])) { - foreach ($options['foreignKeys'] as $fk) { - $local = $fk['local']; - $found = false; - if (isset($options['indexes'])) { - foreach ($options['indexes'] as $definition) { - if (is_string($definition['fields'])) { - // Check if index already exists on the column - $found = ($local == $definition['fields']); - } else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) { - // Index already exists on the column - $found = true; - } - } - } - if (isset($options['primary']) && !empty($options['primary']) && - in_array($local, $options['primary'])) { - // field is part of the PK and therefore already indexed - $found = true; - } - - if ( ! $found) { - $options['indexes'][$local] = array('fields' => array($local => array())); - } - } - }*/ - if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { foreach ($options['uniqueConstraints'] as $uniqueConstraint) { $queryFields .= ', UNIQUE(' . implode(', ', array_values($uniqueConstraint)) . ')'; @@ -764,16 +736,14 @@ class MySqlPlatform extends AbstractPlatform } } - if ( ! isset($definition['fields'])) { + if ( ! isset($definition['columns'])) { throw DoctrineException::indexFieldsArrayRequired(); } - if ( ! is_array($definition['fields'])) { - $definition['fields'] = array($definition['fields']); - } + $definition['columns'] = (array) $definition['columns']; $query = $type . 'INDEX ' . $name; - $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['fields']) . ')'; + $query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')'; return $query; } diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index 51b2f30de..da2f85200 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -314,7 +314,7 @@ class OraclePlatform extends AbstractPlatform $indexName = $table . '_AI_PK'; $definition = array( 'primary' => true, - 'fields' => array($name => true), + 'columns' => array($name => true), ); $sql[] = 'DECLARE diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 6b5998c1d..4d65ceaa9 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -397,7 +397,7 @@ abstract class AbstractSchemaManager * * Example * array( - * 'fields' => array( + * 'columns' => array( * 'user_name' => array(), * 'last_login' => array() * ) @@ -430,7 +430,7 @@ abstract class AbstractSchemaManager * * Example * array( - * 'fields' => array( + * 'columns' => array( * 'user_name' => array( * 'sorting' => 'ascending' * ), @@ -481,7 +481,7 @@ abstract class AbstractSchemaManager * * Example * array( - * 'fields' => array( + * 'columns' => array( * 'user_name' => array(), * 'last_login' => array() * ) @@ -518,7 +518,7 @@ abstract class AbstractSchemaManager * * Example * array( - * 'fields' => array( + * 'columns' => array( * 'user_name' => array( * 'sorting' => 'ascending' * ), diff --git a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php index de8de8eed..3650ace23 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/AnnotationDriver.php @@ -85,7 +85,7 @@ class AnnotationDriver implements Driver if ($tableAnnot->indexes !== null) { foreach ($tableAnnot->indexes as $indexAnnot) { - $primaryTable['indexes'][$indexAnnot->name] = array('fields' => $indexAnnot->columns); + $primaryTable['indexes'][$indexAnnot->name] = array('columns' => $indexAnnot->columns); } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php index 103456e2d..0a48834d0 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php @@ -95,8 +95,13 @@ class XmlDriver extends AbstractFileDriver // Evaluate if (isset($xmlRoot->indexes)) { foreach ($xmlRoot->indexes->index as $index) { + if (is_string($index['columns'])) { + $columns = explode(',', $index['columns']); + } else { + $columns = $index['columns']; + } $metadata->primaryTable['indexes'][$index['name']] = array( - 'fields' => explode(',', $index['columns']) + 'columns' => $columns ); } } @@ -104,7 +109,12 @@ class XmlDriver extends AbstractFileDriver // Evaluate if (isset($xmlRoot->{'unique-constraints'})) { foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) { - $metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']); + if (is_string($unique['columns'])) { + $columns = explode(',', $unique['columns']); + } else { + $columns = $unique['columns']; + } + $metadata->primaryTable['uniqueConstraints'][] = $columns; } } diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index e29aaac94..00db64940 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -97,9 +97,17 @@ class YamlDriver extends AbstractFileDriver // Evaluate indexes if (isset($element['indexes'])) { - foreach ($element['indexes'] as $index) { + foreach ($element['indexes'] as $name => $index) { + if ( ! isset($index['name'])) { + $index['name'] = $name; + } + if (is_string($index['columns'])) { + $columns = explode(',', $index['columns']); + } else { + $columns = $index['columns']; + } $metadata->primaryTable['indexes'][$index['name']] = array( - 'fields' => explode(',', $index['columns']) + 'columns' => $columns ); } } @@ -107,7 +115,12 @@ class YamlDriver extends AbstractFileDriver // Evaluate uniqueConstraints if (isset($element['uniqueConstraints'])) { foreach ($element['uniqueConstraints'] as $unique) { - $metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']); + if (is_string($index['columns'])) { + $columns = explode(',', $unique['columns']); + } else { + $columns = $unique['columns']; + } + $metadata->primaryTable['uniqueConstraints'][] = $columns; } } diff --git a/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php b/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php index a695ef10f..f51bff28b 100644 --- a/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php +++ b/lib/Doctrine/ORM/Tools/Cli/Tasks/ConvertMappingTask.php @@ -23,6 +23,13 @@ namespace Doctrine\ORM\Tools\Cli\Tasks; use Doctrine\ORM\Tools\Export\ClassMetadataExporter; +if ( ! class_exists('sfYaml', false)) { + require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYaml.class.php'; + require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlDumper.class.php'; + require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlInline.class.php'; + require_once __DIR__ . '/../../../../../vendor/sfYaml/sfYamlParser.class.php'; +} + /** * CLI Task to convert your mapping information between the various formats * @@ -103,30 +110,52 @@ class ConvertMappingTask extends AbstractTask $args = $this->getArguments(); $cme = new ClassMetadataExporter(); - $from = (array) $args['from']; - foreach ($from as $path) { - $type = $this->_determinePathType($path); - $printer->writeln(sprintf('Adding %s mapping directory: "%s"', $type, $path), 'INFO'); + // Get exporter and configure it + $exporter = $cme->getExporter($args['to'], $args['dest']); - $cme->addMappingDir($path, $type); - } - - $exporter = $cme->getExporter($args['to']); if (isset($args['extend'])) { $exporter->setClassToExtend($args['extend']); } if (isset($args['num-spaces'])) { $exporter->setNumSpaces($args['num-spaces']); } - $exporter->setOutputDir($args['dest']); + + $from = (array) $args['from']; + + if ($this->_isDoctrine1Schema($from)) { + $printer->writeln('Converting Doctrine 1 schema to Doctrine 2 mapping files', 'INFO'); + + $converter = new \Doctrine\ORM\Tools\ConvertDoctrine1Schema($from); + $exporter->setMetadatas($converter->getMetadatasFromSchema()); + } else { + foreach ($from as $path) { + $type = $this->_determinePathType($path); + + $printer->writeln(sprintf('Adding %s mapping directory: "%s"', $type, $path), 'INFO'); + + $cme->addMappingDir($path, $type); + } + $exporter->setMetadatas($cme->getMetadatasForMappingDirectories()); + } $printer->writeln(sprintf('Exporting %s mapping information to directory: "%s"', $args['to'], $args['dest']), 'INFO'); - $exporter->export(); } - protected function _determinePathType($path) + private function _isDoctrine1Schema(array $from) + { + $files = glob($from[0] . '/*.yml'); + if ($files) { + $array = \sfYaml::load($files[0]); + $first = current($array); + return isset($first['columns']); + } else { + return false; + } + } + + private function _determinePathType($path) { $files = glob($path . '/*.*'); if (!$files) diff --git a/lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php b/lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php new file mode 100644 index 000000000..e62983dd6 --- /dev/null +++ b/lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php @@ -0,0 +1,259 @@ +. + */ + +namespace Doctrine\ORM\Tools; + +use Doctrine\ORM\Mapping\ClassMetadataInfo, + Doctrine\ORM\Tools\Export\Driver\AbstractExporter, + Doctrine\Common\Util\Inflector; + +if ( ! class_exists('sfYaml', false)) { + require_once __DIR__ . '/../../../vendor/sfYaml/sfYaml.class.php'; + require_once __DIR__ . '/../../../vendor/sfYaml/sfYamlDumper.class.php'; + require_once __DIR__ . '/../../../vendor/sfYaml/sfYamlInline.class.php'; + require_once __DIR__ . '/../../../vendor/sfYaml/sfYamlParser.class.php'; +} + +/** + * Class to help with converting Doctrine 1 schema files to Doctrine 2 mapping files + * + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @link www.doctrine-project.org + * @since 2.0 + * @version $Revision$ + * @author Guilherme Blanco + * @author Jonathan Wage + * @author Roman Borschel + */ +class ConvertDoctrine1Schema +{ + /** + * Constructor passes the directory or array of directories + * to convert the Doctrine 1 schema files from + * + * @param string $from + * @author Jonathan Wage + */ + public function __construct($from) + { + $this->_from = (array) $from; + } + + /** + * Get an array of ClassMetadataInfo instances from the passed + * Doctrine 1 schema + * + * @return array $metadatas An array of ClassMetadataInfo instances + */ + public function getMetadatasFromSchema() + { + $schema = array(); + foreach ($this->_from as $path) { + if (is_dir($path)) { + $files = glob($path . '/*.yml'); + foreach ($files as $file) { + $schema = array_merge($schema, (array) \sfYaml::load($file)); + } + } else { + $schema = array_merge($schema, (array) \sfYaml::load($path)); + } + } + + $metadatas = array(); + foreach ($schema as $name => $model) { + $metadatas[] = $this->_convertToClassMetadataInfo($name, $model); + } + + return $metadatas; + } + + private function _convertToClassMetadataInfo($modelName, $model) + { + $metadata = new ClassMetadataInfo($modelName); + + $this->_convertTableName($modelName, $model, $metadata); + $this->_convertColumns($modelName, $model, $metadata); + $this->_convertIndexes($modelName, $model, $metadata); + $this->_convertRelations($modelName, $model, $metadata); + + return $metadata; + } + + private function _convertTableName($modelName, array $model, ClassMetadataInfo $metadata) + { + if (isset($model['tableName']) && $model['tableName']) { + $e = explode('.', $model['tableName']); + if (count($e) > 1) { + $metadata->primaryTable['schema'] = $e[0]; + $metadata->primaryTable['name'] = $e[1]; + } else { + $metadata->primaryTable['name'] = $e[0]; + } + } + } + + private function _convertColumns($modelName, array $model, ClassMetadataInfo $metadata) + { + $id = false; + + if (isset($model['columns']) && $model['columns']) { + foreach ($model['columns'] as $name => $column) { + $fieldMapping = $this->_convertColumn($modelName, $name, $column, $metadata); + + if (isset($fieldMapping['id']) && $fieldMapping['id']) { + $id = true; + } + } + } + + if ( ! $id) { + $fieldMapping = array( + 'fieldName' => 'id', + 'columnName' => 'id', + 'type' => 'integer', + 'id' => true + ); + $metadata->mapField($fieldMapping); + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); + } + } + + private function _convertColumn($modelName, $name, $column, ClassMetadataInfo $metadata) + { + if (is_string($column)) { + $string = $column; + $column = array(); + $column['type'] = $string; + } + if (preg_match("/([a-zA-Z]+)\(([0-9]+)\)/", $column['type'], $matches)) { + $column['type'] = $matches[1]; + $column['length'] = $matches[2]; + } + if ( ! isset($column['name'])) { + $column['name'] = $name; + } + $fieldMapping = array(); + if (isset($column['primary'])) { + $fieldMapping['id'] = true; + } + $fieldMapping['fieldName'] = isset($column['alias']) ? $column['alias'] : $name; + $fieldMapping['columnName'] = $column['name']; + $fieldMapping['type'] = $column['type']; + if (isset($column['length'])) { + $fieldMapping['length'] = $column['length']; + } + $allowed = array('precision', 'scale', 'unique', 'options', 'notnull', 'version'); + foreach ($column as $key => $value) { + if (in_array($key, $allowed)) { + $fieldMapping[$key] = $value; + } + } + + $metadata->mapField($fieldMapping); + + if (isset($column['autoincrement'])) { + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); + } else if (isset($column['sequence'])) { + $metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE); + $metadata->setSequenceGeneratorDefinition($definition); + $definition = array( + 'sequenceName' => is_array($column['sequence']) ? $column['sequence']['name']:$column['sequence'] + ); + if (isset($column['sequence']['size'])) { + $definition['allocationSize'] = $column['sequence']['size']; + } + if (isset($column['sequence']['value'])) { + $definition['initialValue'] = $column['sequence']['value']; + } + } + return $fieldMapping; + } + + private function _convertIndexes($modelName, array $model, ClassMetadataInfo $metadata) + { + if (isset($model['indexes']) && $model['indexes']) { + foreach ($model['indexes'] as $name => $index) { + $metadata->primaryTable['indexes'][$name] = array( + 'columns' => $index['fields'] + ); + + if (isset($index['type']) && $index['type'] == 'unique') { + $metadata->primaryTable['uniqueConstraints'][] = $index['fields']; + } + } + } + } + + private function _convertRelations($modelName, array $model, ClassMetadataInfo $metadata) + { + if (isset($model['relations']) && $model['relations']) { + foreach ($model['relations'] as $name => $relation) { + if ( ! isset($relation['alias'])) { + $relation['alias'] = $name; + } + if ( ! isset($relation['class'])) { + $relation['class'] = $name; + } + if ( ! isset($relation['local'])) { + $relation['local'] = Inflector::tableize($relation['class']); + } + if ( ! isset($relation['foreign'])) { + $relation['foreign'] = 'id'; + } + if ( ! isset($relation['foreignAlias'])) { + $relation['foreignAlias'] = $modelName; + } + + if (isset($relation['refClass'])) { + $type = 'many'; + $foreignType = 'many'; + } else { + $type = isset($relation['type']) ? $relation['type'] : 'one'; + $foreignType = isset($relation['foreignType']) ? $relation['foreignType'] : 'many'; + $joinColumns = array( + array( + 'name' => $relation['local'], + 'referencedColumnName' => $relation['foreign'], + 'onDelete' => isset($relation['onDelete']) ? $relation['onDelete'] : null, + 'onUpdate' => isset($relation['onUpdate']) ? $relation['onUpdate'] : null, + ) + ); + } + + if ($type == 'one' && $foreignType == 'one') { + $method = 'mapOneToOne'; + } else if ($type == 'many' && $foreignType == 'many') { + $method = 'mapManyToMany'; + } else { + $method = 'mapOneToMany'; + } + + $associationMapping = array(); + $associationMapping['fieldName'] = $relation['alias']; + $associationMapping['targetEntity'] = $relation['class']; + $associationMapping['mappedBy'] = $relation['foreignAlias']; + $associationMapping['joinColumns'] = $joinColumns; + + $metadata->$method($associationMapping); + } + } + } +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php index 29ae5d5f0..41f5725f7 100644 --- a/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/ClassMetadataExporter.php @@ -30,18 +30,20 @@ use Doctrine\ORM\Mapping\ClassMetadata; * Class used for converting your mapping information between the * supported formats: yaml, xml, and php/annotation. * - * [php] - * // Unify all your mapping information which is written in php, xml, yml - * // and convert it to a single set of yaml files. + * [php] + * // Unify all your mapping information which is written in php, xml, yml + * // and convert it to a single set of yaml files. + * + * $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter(); + * $cme->addMappingDirectory(__DIR__ . '/Entities', 'php'); + * $cme->addMappingDirectory(__DIR__ . '/xml', 'xml'); + * $cme->addMappingDirectory(__DIR__ . '/yaml', 'yaml'); + * + * $exporter = $cme->getExporter('yaml'); + * $exporter->setOutputDir(__DIR__ . '/new_yaml'); * - * $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter(); - * $cme->addMappingDir(__DIR__ . '/Entities', 'php'); - * $cme->addMappingDir(__DIR__ . '/xml', 'xml'); - * $cme->addMappingDir(__DIR__ . '/yaml', 'yaml'); - * - * $exporter = $cme->getExporter('yaml'); - * $exporter->setOutputDir(__DIR__ . '/new_yaml'); - * $exporter->export(); + * $exporter->setMetadatas($cme->getMetadatasForMappingDirectories()); + * $exporter->export(); * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link www.doctrine-project.org @@ -68,7 +70,19 @@ class ClassMetadataExporter private $_mappingDirectories = array(); - public function addMappingDir($dir, $type) + /** + * Add a new mapping directory to the array of directories to convert and export + * to another format + * + * [php] + * $cme = new Doctrine\ORM\Tools\Export\ClassMetadataExporter(); + * $cme->addMappingDirectory(__DIR__ . '/yaml', 'yaml'); + * + * @param string $dir The path to the mapping files + * @param string $type The type of mapping files (yml, xml, etc.) + * @return void + */ + public function addMappingDirectory($dir, $type) { if ($type === 'php') { $this->_mappingDirectories[] = array($dir, $type); @@ -82,13 +96,23 @@ class ClassMetadataExporter } } - public function getMappingDriver($type, $dir) + /** + * Get an instance of a mapping driver + * + * @param string $type The type of mapping driver (yaml, xml, annotation, etc.) + * @param string $dir The directory to configure the driver to look in. Only required for file drivers (yml, xml). + * @return AbstractDriver $driver + */ + public function getMappingDriver($type, $dir = null) { if ( ! isset($this->_mappingDrivers[$type])) { return false; } $class = $this->_mappingDrivers[$type]; if (is_subclass_of($class, 'Doctrine\ORM\Mapping\Driver\AbstractFileDriver')) { + if (is_null($dir)) { + throw DoctrineException::fileMappingDriversRequireDirectoryPath(); + } $driver = new $class($dir, constant($class . '::PRELOAD')); } else { $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache); @@ -98,12 +122,24 @@ class ClassMetadataExporter return $driver; } + /** + * Get the array of added mapping directories + * + * @return array $mappingDirectories + */ public function getMappingDirectories() { return $this->_mappingDirectories; } - public function getMetadataInstances() + /** + * Get an array of ClassMetadataInfo instances for all the configured mapping + * directories. Reads the mapping directories and populates ClassMetadataInfo + * instances. + * + * @return array $classes + */ + public function getMetadatasForMappingDirectories() { $classes = array(); @@ -168,6 +204,13 @@ class ClassMetadataExporter return $classes; } + /** + * Get a exporter driver instance + * + * @param string $type The type to get (yml, xml, etc.) + * @param string $dir The directory where the exporter will export to + * @return AbstractExporter $exporter + */ public function getExporter($type, $dir = null) { if ( ! isset($this->_exporterDrivers[$type])) { @@ -175,6 +218,6 @@ class ClassMetadataExporter } $class = $this->_exporterDrivers[$type]; - return new $class($this->getMetadataInstances(), $dir); + return new $class($dir); } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php index 498fc7078..b8321ec44 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/AbstractExporter.php @@ -40,10 +40,14 @@ abstract class AbstractExporter protected $_outputDir; protected $_extension; - public function __construct(array $metadatas, $dir = null) + public function __construct($dir = null) + { + $this->_outputDir = $dir; + } + + public function setMetadatas(array $metadatas) { $this->_metadatas = $metadatas; - $this->_outputDir = $dir; } public function getExtension() diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php index df16940dc..45c6afb75 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/AnnotationExporter.php @@ -37,36 +37,19 @@ use Doctrine\ORM\Mapping\ClassMetadataInfo, class AnnotationExporter extends AbstractExporter { protected $_extension = '.php'; - protected $_isNew = false; - protected $_outputPath; - protected $_numSpaces = 4; - protected $_classToExtend; - protected $_currentCode; - public function hasProperty($property, $metadata) - { - if ($this->_isNew) { - return false; - } else { - return strpos($this->_currentCode, '$' . $property) !== false ? true : false; - } - } - - public function hasMethod($method, $metadata) - { - if ($this->_isNew) { - return false; - } else { - return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false; - } - } + private $_isNew = false; + private $_outputPath; + private $_numSpaces = 4; + private $_classToExtend; + private $_currentCode; /** * Converts a single ClassMetadata instance to the exported format * and returns it * * @param ClassMetadataInfo $metadata - * @return mixed $exported + * @return string $exported */ public function exportClassMetadata(ClassMetadataInfo $metadata) { @@ -106,49 +89,108 @@ class AnnotationExporter extends AbstractExporter } } $code = array_values($code); - $code = implode("\n", $code); + $exported = implode("\n", $code); - return $code; + return $exported; } + /** + * Set the number of spaces the exported class should have + * + * @param integer $numSpaces + * @return void + */ public function setNumSpaces($numSpaces) { $this->_numSpaces = $numSpaces; } - public function hasNamespace($metadata) - { - return strpos($metadata->name, '\\') ? true : false; - } - - public function extendsClass() - { - return $this->_classToExtend ? true : false; - } - - public function getClassToExtend() - { - return $this->_classToExtend; - } - + /** + * Set the name of the class the generated classes should extend from + * + * @return void + */ public function setClassToExtend($classToExtend) { $this->_classToExtend = $classToExtend; } - public function getClassToExtendName() + /** + * This method is overriden so that each class is outputted + * to the appropriate path where namespaces become directories. + * + * Export each ClassMetadata instance to a single Doctrine Mapping file + * named after the entity + * + * @return void + */ + public function export() + { + if ( ! is_dir($this->_outputDir)) { + mkdir($this->_outputDir, 0777); + } + + foreach ($this->_metadatas as $metadata) { + $outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension; + $outputDir = dirname($outputPath); + if ( ! is_dir($outputDir)) { + mkdir($outputDir, 0777, true); + } + if ( ! file_exists($outputPath)) { + $this->_isNew = true; + } + $this->_outputPath = $outputPath; + $output = $this->exportClassMetadata($metadata); + file_put_contents($outputPath, $output); + } + } + + private function _hasProperty($property, $metadata) + { + if ($this->_isNew) { + return false; + } else { + return strpos($this->_currentCode, '$' . $property) !== false ? true : false; + } + } + + private function _hasMethod($method, $metadata) + { + if ($this->_isNew) { + return false; + } else { + return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false; + } + } + + private function _hasNamespace($metadata) + { + return strpos($metadata->name, '\\') ? true : false; + } + + private function _extendsClass() + { + return $this->_classToExtend ? true : false; + } + + private function _getClassToExtend() + { + return $this->_classToExtend; + } + + private function _getClassToExtendName() { $refl = new \ReflectionClass($this->getClassToExtend()); return $refl->getShortName(); } - public function getClassToExtendNamespace() + private function _getClassToExtendNamespace() { $refl = new \ReflectionClass($this->getClassToExtend()); return $refl->getNamespaceName() ? $refl->getNamespaceName():$refl->getShortName(); } - public function getClassName($metadata) + private function _getClassName($metadata) { if ($pos = strpos($metadata->name, '\\')) { return substr($metadata->name, $pos + 1, strlen($metadata->name)); @@ -157,15 +199,15 @@ class AnnotationExporter extends AbstractExporter } } - public function getNamespace($metadata) + private function _getNamespace($metadata) { return substr($metadata->name, 0, strrpos($metadata->name, '\\')); } - public function addMethod($type, $fieldName, $metadata, array &$methods) + private function _addMethod($type, $fieldName, $metadata, array &$methods) { $methodName = $type . ucfirst($fieldName); - if ($this->hasMethod($methodName, $metadata)) { + if ($this->_hasMethod($methodName, $metadata)) { return false; } @@ -203,37 +245,37 @@ class AnnotationExporter extends AbstractExporter $methods[] = implode("\n", $method); } - public function getMethods($metadata) + private function _getMethods($metadata) { $methods = array(); foreach ($metadata->fieldMappings as $fieldMapping) { - $this->addMethod('set', $fieldMapping['fieldName'], $metadata, $methods); - $this->addMethod('get', $fieldMapping['fieldName'], $metadata, $methods); + $this->_addMethod('set', $fieldMapping['fieldName'], $metadata, $methods); + $this->_addMethod('get', $fieldMapping['fieldName'], $metadata, $methods); } foreach ($metadata->associationMappings as $associationMapping) { if ($associationMapping instanceof \Doctrine\ORM\Mapping\OneToOneMapping) { - $this->addMethod('set', $associationMapping->sourceFieldName, $metadata, $methods); - $this->addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('set', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); } else if ($associationMapping instanceof \Doctrine\ORM\Mapping\OneToManyMapping) { if ($associationMapping->isOwningSide) { - $this->addMethod('set', $associationMapping->sourceFieldName, $metadata, $methods); - $this->addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('set', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); } else { - $this->addMethod('add', $associationMapping->sourceFieldName, $metadata, $methods); - $this->addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('add', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); } } else if ($associationMapping instanceof \Doctrine\ORM\Mapping\ManyToManyMapping) { - $this->addMethod('add', $associationMapping->sourceFieldName, $metadata, $methods); - $this->addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('add', $associationMapping->sourceFieldName, $metadata, $methods); + $this->_addMethod('get', $associationMapping->sourceFieldName, $metadata, $methods); } } return $methods; } - public function getTableAnnotation($metadata) + private function _getTableAnnotation($metadata) { $table = array(); $table[] = 'name="' . $metadata->primaryTable['name'] . '"'; @@ -243,7 +285,7 @@ class AnnotationExporter extends AbstractExporter return '@Table(' . implode(', ', $table) . ')'; } - public function getAssociationMappingAnnotation(AssociationMapping $associationMapping, ClassMetadataInfo $metadata) + private function _getAssociationMappingAnnotation(AssociationMapping $associationMapping, ClassMetadataInfo $metadata) { // TODO: This function still needs to be written :) $lines = array(); @@ -254,7 +296,7 @@ class AnnotationExporter extends AbstractExporter return implode("\n", $lines); } - public function getFieldMappingAnnotation(array $fieldMapping, ClassMetadataInfo $metadata) + private function _getFieldMappingAnnotation(array $fieldMapping, ClassMetadataInfo $metadata) { $lines = array(); $lines[] = str_repeat(' ', $this->_numSpaces) . '/**'; @@ -314,31 +356,4 @@ class AnnotationExporter extends AbstractExporter return implode("\n", $lines); } - - /** - * Export each ClassMetadata instance to a single Doctrine Mapping file - * named after the entity - * - * @return void - */ - public function export() - { - if ( ! is_dir($this->_outputDir)) { - mkdir($this->_outputDir, 0777); - } - - foreach ($this->_metadatas as $metadata) { - $outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension; - $outputDir = dirname($outputPath); - if ( ! is_dir($outputDir)) { - mkdir($outputDir, 0777, true); - } - if ( ! file_exists($outputPath)) { - $this->_isNew = true; - } - $this->_outputPath = $outputPath; - $output = $this->exportClassMetadata($metadata); - file_put_contents($outputPath, $output); - } - } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php index b0d47201d..c2cf334c5 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/XmlExporter.php @@ -102,7 +102,7 @@ class XmlExporter extends AbstractExporter foreach ($metadata->primaryTable['indexes'] as $name => $index) { $indexXml = $root->addChild('index'); $indexXml->addAttribute('name', $name); - $indexXml->addAttribute('columns', implode(',', $index['fields'])); + $indexXml->addAttribute('columns', implode(',', $index['columns'])); } } @@ -110,7 +110,7 @@ class XmlExporter extends AbstractExporter $uniqueConstraintsXml = $root->addChild('unique-constraints'); foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) { $uniqueConstraintXml = $uniqueConstraintsXml->addChild('unique-constraint'); - $uniqueConstraintXml->addAttribute('columns', $uniqueConstraint['fields']); + $uniqueConstraintXml->addAttribute('columns', $uniqueConstraint['columns']); } } diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php index 3b9a6bdc8..0d5c1a15d 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/YamlExporter.php @@ -87,7 +87,9 @@ class YamlExporter extends AbstractExporter } if (isset($metadata->primaryTable['uniqueConstraints'])) { - $array['uniqueConstraints'] = $metadata->primaryTable['uniqueConstraints']; + foreach ($metadata->primaryTable['uniqueConstraints'] as $uniqueConstraint) { + $array['uniqueConstraints'][]['columns'] = $uniqueConstraint; + } } $fields = $metadata->fieldMappings; diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php b/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php index 1bac1c96f..2742eb757 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/annotation.tpl.php @@ -1,12 +1,12 @@ [?php -hasNamespace($metadata)): ?> +_hasNamespace($metadata)): ?> -namespace getNamespace($metadata) ?>; +namespace _getNamespace($metadata) ?>; -extendsClass()): ?> +_extendsClass()): ?> -use getClassToExtendNamespace() ?>; +use _getClassToExtendNamespace() ?>; /** @@ -15,9 +15,9 @@ use getClassToExtendNamespace() ?>; * @Entity - * getTableAnnotation($metadata)."\n" ?> + * _getTableAnnotation($metadata)."\n" ?> */ -class getClassName($metadata) ?>extendsClass()): ?> extends getClassToExtendName() ?> +class _getClassName($metadata) ?>_extendsClass()): ?> extends _getClassToExtendName() ?> { diff --git a/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php b/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php index 100cd985c..8f0821a71 100644 --- a/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php +++ b/lib/Doctrine/ORM/Tools/Export/Driver/annotation_body.tpl.php @@ -1,13 +1,13 @@ -fieldMappings as $fieldMapping): ?>hasProperty($fieldMapping['fieldName'], $metadata)) continue; ?> -getFieldMappingAnnotation($fieldMapping, $metadata)."\n" ?> +fieldMappings as $fieldMapping): ?>_hasProperty($fieldMapping['fieldName'], $metadata)) continue; ?> +_getFieldMappingAnnotation($fieldMapping, $metadata)."\n" ?> _numSpaces) ?>private $; -associationMappings as $associationMapping): ?>hasProperty($associationMapping->sourceFieldName, $metadata)) continue; ?> -getAssociationMappingAnnotation($associationMapping, $metadata)."\n" ?> +associationMappings as $associationMapping): ?>_hasProperty($associationMapping->sourceFieldName, $metadata)) continue; ?> +_getAssociationMappingAnnotation($associationMapping, $metadata)."\n" ?> _numSpaces) ?>private $sourceFieldName ?>isManyToMany()): ?> = array() ; -getMethods($metadata) as $method): ?> +_getMethods($metadata) as $method): ?> \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php index 0e29cb846..4acbb5f98 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/MySqlSchemaManagerTest.php @@ -75,7 +75,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase $data['options'] = array( 'indexes' => array( 'test_index_name' => array( - 'fields' => array( + 'columns' => array( 'test' => array() ), 'type' => 'unique' diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php index 76199bf9b..4cc1586f5 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php @@ -71,7 +71,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase $data['options'] = array( 'indexes' => array( 'test_index_name' => array( - 'fields' => array( + 'columns' => array( 'test' => array() ), 'type' => 'unique' diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/PostgreSqlSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/PostgreSqlSchemaManagerTest.php index d944aafad..33426a0d4 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/PostgreSqlSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/PostgreSqlSchemaManagerTest.php @@ -73,7 +73,7 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase $data['options'] = array( 'indexes' => array( 'test' => array( - 'fields' => array( + 'columns' => array( 'test' => array() ), 'type' => 'unique' diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php index feb7ef24a..2f525beb7 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SqliteSchemaManagerTest.php @@ -79,7 +79,7 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase $data['options'] = array( 'indexes' => array( 'test' => array( - 'fields' => array( + 'columns' => array( 'test' => array() ), 'type' => 'unique' @@ -169,7 +169,7 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase $this->createTestTable('test_create_index'); $index = array( - 'fields' => array( + 'columns' => array( 'test' => array() ), 'type' => 'unique' diff --git a/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php index 78cf12b1b..c42cafa13 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php @@ -147,14 +147,14 @@ class MsSqlPlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesConstraintCreationSql() { - $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('fields' => array('test' => array()))); + $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('columns' => array('test' => array()))); $this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)'); } public function testGeneratesIndexCreationSql() { $indexDef = array( - 'fields' => array( + 'columns' => array( 'user_name' => array( 'sorting' => 'ASC', 'length' => 10 @@ -171,7 +171,7 @@ class MsSqlPlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesUniqueIndexCreationSql() { - $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'fields' => array('test', 'test2'))); + $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2'))); $this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)'); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php index 04456bace..2e7e11b9f 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php @@ -149,14 +149,14 @@ class MySqlPlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesConstraintCreationSql() { - $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('fields' => array('test' => array()))); + $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('columns' => array('test' => array()))); $this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)'); } public function testGeneratesIndexCreationSql() { $indexDef = array( - 'fields' => array( + 'columns' => array( 'user_name' => array( 'sorting' => 'ASC', 'length' => 10 @@ -173,7 +173,7 @@ class MySqlPlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesUniqueIndexCreationSql() { - $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'fields' => array('test', 'test2'))); + $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2'))); $this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)'); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php index 0fe977e3f..60794b7d3 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php @@ -186,14 +186,14 @@ class OraclePlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesConstraintCreationSql() { - $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('fields' => array('test' => array()))); + $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('columns' => array('test' => array()))); $this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)'); } public function testGeneratesIndexCreationSql() { $indexDef = array( - 'fields' => array( + 'columns' => array( 'user_name' => array( 'sorting' => 'ASC', 'length' => 10 @@ -210,7 +210,7 @@ class OraclePlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesUniqueIndexCreationSql() { - $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'fields' => array('test', 'test2'))); + $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2'))); $this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)'); } diff --git a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php index 69a86b264..e6f37518f 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php @@ -66,7 +66,7 @@ class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesIndexCreationSql() { $indexDef = array( - 'fields' => array( + 'columns' => array( 'user_name', 'last_login' ) diff --git a/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php index 025eb110d..5bf0a205a 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php @@ -95,13 +95,13 @@ class SqlitePlatformTest extends \Doctrine\Tests\DbalTestCase public function testGeneratesConstraintCreationSql() { - $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('fields' => array('test' => array()))); + $sql = $this->_platform->getCreateConstraintSql('test', 'constraint_name', array('columns' => array('test' => array()))); $this->assertEquals('ALTER TABLE test ADD CONSTRAINT constraint_name (test)', $sql); } public function testGeneratesIndexCreationSql() { - $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'fields' => array('test', 'test2'))); + $sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2'))); $this->assertEquals('CREATE UNIQUE INDEX index_name ON test (test, test2)', $sql); } diff --git a/tests/Doctrine/Tests/ORM/Tools/AllTests.php b/tests/Doctrine/Tests/ORM/Tools/AllTests.php index 5d6ac3c07..9f837d05e 100644 --- a/tests/Doctrine/Tests/ORM/Tools/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Tools/AllTests.php @@ -21,6 +21,7 @@ class AllTests $suite = new \Doctrine\Tests\DoctrineTestSuite('Doctrine Orm Hydration'); $suite->addTestSuite('Doctrine\Tests\ORM\Tools\Export\ClassMetadataExporterTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Tools\ConvertDoctrine1SchemaTest'); return $suite; } diff --git a/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php new file mode 100644 index 000000000..5721e45fe --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/ConvertDoctrine1SchemaTest.php @@ -0,0 +1,71 @@ +. + */ + +namespace Doctrine\Tests\ORM\Tools; + +use Doctrine\ORM\Tools\Export\ClassMetadataExporter, + Doctrine\ORM\Tools\ConvertDoctrine1Schema; + +require_once __DIR__ . '/../../TestInit.php'; + +/** + * Test case for converting a Doctrine 1 style schema to Doctrine 2 mapping files + * + * @author Jonathan H. Wage + * @author Roman Borschel getExporter('yml', __DIR__ . '/convert'); + $exporter->setMetadatas($converter->getMetadatasFromSchema()); + $exporter->export(); + + $this->assertTrue(file_exists(__DIR__ . '/convert/User.dcm.yml')); + $this->assertTrue(file_exists(__DIR__ . '/convert/Profile.dcm.yml')); + + $cme->addMappingDirectory(__DIR__ . '/convert', 'yml'); + $metadatas = $cme->getMetadatasForMappingDirectories(); + + $this->assertEquals(2, count($metadatas)); + $this->assertEquals('Profile', $metadatas[0]->name); + $this->assertEquals('User', $metadatas[1]->name); + $this->assertEquals(4, count($metadatas[0]->fieldMappings)); + $this->assertEquals(3, count($metadatas[1]->fieldMappings)); + + $this->assertEquals('Profile', $metadatas[0]->associationMappings['User']->sourceEntityName); + $this->assertEquals('\User', $metadatas[0]->associationMappings['User']->targetEntityName); + + $this->assertEquals('username', $metadatas[1]->primaryTable['indexes']['username']['columns'][0]); + + unlink(__DIR__ . '/convert/User.dcm.yml'); + unlink(__DIR__ . '/convert/Profile.dcm.yml'); + rmdir(__DIR__ . '/convert'); + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTest.php b/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTest.php index abf727b18..fe3e3b23a 100644 --- a/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/Export/ClassMetadataExporterTest.php @@ -61,10 +61,10 @@ class ClassMetadataExporterTest extends \Doctrine\Tests\OrmTestCase public function testAddMappingDirectory() { $cme = new ClassMetadataExporter(); - $cme->addMappingDir(__DIR__ . '/annotation', 'annotation'); - $cme->addMappingDir(__DIR__ . '/php', 'php'); - $cme->addMappingDir(__DIR__ . '/xml', 'xml'); - $cme->addMappingDir(__DIR__ . '/yml', 'yml'); + $cme->addMappingDirectory(__DIR__ . '/annotation', 'annotation'); + $cme->addMappingDirectory(__DIR__ . '/php', 'php'); + $cme->addMappingDirectory(__DIR__ . '/xml', 'xml'); + $cme->addMappingDirectory(__DIR__ . '/yml', 'yml'); $mappingDirectories = $cme->getMappingDirectories(); $this->assertEquals(4, count($mappingDirectories)); @@ -89,11 +89,11 @@ class ClassMetadataExporterTest extends \Doctrine\Tests\OrmTestCase public function testGetMetadataInstances() { $cme = new ClassMetadataExporter(); - $cme->addMappingDir(__DIR__ . '/php', 'php'); - $cme->addMappingDir(__DIR__ . '/xml', 'xml'); - $cme->addMappingDir(__DIR__ . '/yml', 'yml'); + $cme->addMappingDirectory(__DIR__ . '/php', 'php'); + $cme->addMappingDirectory(__DIR__ . '/xml', 'xml'); + $cme->addMappingDirectory(__DIR__ . '/yml', 'yml'); - $metadataInstances = $cme->getMetadataInstances(); + $metadataInstances = $cme->getMetadatasForMappingDirectories(); $this->assertEquals(3, count($metadataInstances)); $this->assertEquals('PhpTest', $metadataInstances[0]->name); @@ -116,13 +116,14 @@ class ClassMetadataExporterTest extends \Doctrine\Tests\OrmTestCase $types = array('annotation', 'php', 'xml', 'yml'); $cme = new ClassMetadataExporter(); - $cme->addMappingDir(__DIR__ . '/php', 'php'); - $cme->addMappingDir(__DIR__ . '/xml', 'xml'); - $cme->addMappingDir(__DIR__ . '/yml', 'yml'); + $cme->addMappingDirectory(__DIR__ . '/php', 'php'); + $cme->addMappingDirectory(__DIR__ . '/xml', 'xml'); + $cme->addMappingDirectory(__DIR__ . '/yml', 'yml'); foreach ($types as $type) { // Export the above mapping directories to the type $exporter = $cme->getExporter($type, __DIR__ . '/export/' . $type); + $exporter->setMetadatas($cme->getMetadatasForMappingDirectories()); $exporter->export(); // Make sure the files were written @@ -132,8 +133,8 @@ class ClassMetadataExporterTest extends \Doctrine\Tests\OrmTestCase // Try and read back in the exported mapping files to make sure they are valid $cme2 = new ClassMetadataExporter(); - $cme2->addMappingDir(__DIR__ . '/export/' . $type, $type); - $metadataInstances = $cme2->getMetadataInstances(); + $cme2->addMappingDirectory(__DIR__ . '/export/' . $type, $type); + $metadataInstances = $cme2->getMetadatasForMappingDirectories(); $this->assertEquals(3, count($metadataInstances)); $this->assertEquals('PhpTest', $metadataInstances[0]->name); $this->assertEquals('XmlTest', $metadataInstances[1]->name); diff --git a/tests/Doctrine/Tests/ORM/Tools/doctrine1schema/schema.yml b/tests/Doctrine/Tests/ORM/Tools/doctrine1schema/schema.yml new file mode 100644 index 000000000..f58781ce2 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Tools/doctrine1schema/schema.yml @@ -0,0 +1,26 @@ +User: + tableName: users + columns: + username: + type: string(255) + length: 100 + notnull: true + unique: true + password: + type: string(255) + indexes: + username: + fields: [username] + type: unique + +Profile: + columns: + first_name: string(255) + last_name: string(255) + user_id: integer + relations: + User: + onDelete: CASCADE + onUpdate: CASCADE + foreignType: one + type: one \ No newline at end of file