[2.0] Added functionality to convert a Doctrine 1 schema to Doctrine 2 to help users with upgrading. Lots of other small bug fixes and changes.
This commit is contained in:
parent
71c1fe5221
commit
c8362da494
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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'
|
||||
* ),
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,8 +95,13 @@ class XmlDriver extends AbstractFileDriver
|
||||
// Evaluate <indexes...>
|
||||
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 <unique-constraints..>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
259
lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php
Normal file
259
lib/Doctrine/ORM/Tools/ConvertDoctrine1Schema.php
Normal file
@ -0,0 +1,259 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
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 <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -1,12 +1,12 @@
|
||||
[?php
|
||||
|
||||
<?php if ($this->hasNamespace($metadata)): ?>
|
||||
<?php if ($this->_hasNamespace($metadata)): ?>
|
||||
|
||||
namespace <?php echo $this->getNamespace($metadata) ?>;
|
||||
namespace <?php echo $this->_getNamespace($metadata) ?>;
|
||||
<?php endif; ?>
|
||||
<?php if ($this->extendsClass()): ?>
|
||||
<?php if ($this->_extendsClass()): ?>
|
||||
|
||||
use <?php echo $this->getClassToExtendNamespace() ?>;
|
||||
use <?php echo $this->_getClassToExtendNamespace() ?>;
|
||||
<?php endif; ?>
|
||||
|
||||
/**
|
||||
@ -15,9 +15,9 @@ use <?php echo $this->getClassToExtendNamespace() ?>;
|
||||
<?php else: ?>
|
||||
* @Entity
|
||||
<?php endif; ?>
|
||||
* <?php echo $this->getTableAnnotation($metadata)."\n" ?>
|
||||
* <?php echo $this->_getTableAnnotation($metadata)."\n" ?>
|
||||
*/
|
||||
class <?Php echo $this->getClassName($metadata) ?><?php if ($this->extendsClass()): ?> extends <?php echo $this->getClassToExtendName() ?><?php endif; ?>
|
||||
class <?Php echo $this->_getClassName($metadata) ?><?php if ($this->_extendsClass()): ?> extends <?php echo $this->_getClassToExtendName() ?><?php endif; ?>
|
||||
|
||||
{
|
||||
<?php include('annotation_body.tpl.php') ?>
|
||||
|
@ -1,13 +1,13 @@
|
||||
<?php foreach ($metadata->fieldMappings as $fieldMapping): ?><?php if ($this->hasProperty($fieldMapping['fieldName'], $metadata)) continue; ?>
|
||||
<?php echo $this->getFieldMappingAnnotation($fieldMapping, $metadata)."\n" ?>
|
||||
<?php foreach ($metadata->fieldMappings as $fieldMapping): ?><?php if ($this->_hasProperty($fieldMapping['fieldName'], $metadata)) continue; ?>
|
||||
<?php echo $this->_getFieldMappingAnnotation($fieldMapping, $metadata)."\n" ?>
|
||||
<?php echo str_repeat(' ', $this->_numSpaces) ?>private $<?php echo $fieldMapping['fieldName'] ?>;
|
||||
|
||||
<?php endforeach ?>
|
||||
<?php foreach ($metadata->associationMappings as $associationMapping): ?><?php if ($this->hasProperty($associationMapping->sourceFieldName, $metadata)) continue; ?>
|
||||
<?php echo $this->getAssociationMappingAnnotation($associationMapping, $metadata)."\n" ?>
|
||||
<?php foreach ($metadata->associationMappings as $associationMapping): ?><?php if ($this->_hasProperty($associationMapping->sourceFieldName, $metadata)) continue; ?>
|
||||
<?php echo $this->_getAssociationMappingAnnotation($associationMapping, $metadata)."\n" ?>
|
||||
<?php echo str_repeat(' ', $this->_numSpaces) ?>private $<?php echo $associationMapping->sourceFieldName ?><?php if ($associationMapping->isManyToMany()): ?> = array() <?php endif; ?>;
|
||||
|
||||
<?php endforeach; ?>
|
||||
<?php foreach ($this->getMethods($metadata) as $method): ?>
|
||||
<?php foreach ($this->_getMethods($metadata) as $method): ?>
|
||||
<?php echo $method ?>
|
||||
<?php endforeach ?>
|
@ -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'
|
||||
|
@ -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'
|
||||
|
@ -73,7 +73,7 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
|
||||
$data['options'] = array(
|
||||
'indexes' => array(
|
||||
'test' => array(
|
||||
'fields' => array(
|
||||
'columns' => array(
|
||||
'test' => array()
|
||||
),
|
||||
'type' => 'unique'
|
||||
|
@ -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'
|
||||
|
@ -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)');
|
||||
}
|
||||
|
||||
|
@ -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)');
|
||||
}
|
||||
|
||||
|
@ -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)');
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
|
||||
public function testGeneratesIndexCreationSql()
|
||||
{
|
||||
$indexDef = array(
|
||||
'fields' => array(
|
||||
'columns' => array(
|
||||
'user_name',
|
||||
'last_login'
|
||||
)
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
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 <jonwage@gmail.com>
|
||||
* @author Roman Borschel <roman@code-factory.org
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @link http://www.phpdoctrine.org
|
||||
* @since 2.0
|
||||
* @version $Revision$
|
||||
*/
|
||||
class ConvertDoctrine1SchemaTest extends \Doctrine\Tests\OrmTestCase
|
||||
{
|
||||
public function testTest()
|
||||
{
|
||||
$cme = new ClassMetadataExporter();
|
||||
$converter = new ConvertDoctrine1Schema(__DIR__ . '/doctrine1schema');
|
||||
|
||||
$exporter = $cme->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');
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
26
tests/Doctrine/Tests/ORM/Tools/doctrine1schema/schema.yml
Normal file
26
tests/Doctrine/Tests/ORM/Tools/doctrine1schema/schema.yml
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user