Skip to content

Commit

Permalink
updated sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 23, 2010
1 parent 8c72ac2 commit 5994cb3
Show file tree
Hide file tree
Showing 29 changed files with 360 additions and 451 deletions.
Expand Up @@ -21,12 +21,12 @@

namespace Doctrine\DBAL\Migrations\Configuration;

use Doctrine\Dbal\Connection,
use Doctrine\DBAL\Connection,
Doctrine\DBAL\Migrations\MigrationException,
Doctrine\DBAL\Migrations\Version,
Doctrine\DBAL\Migrations\OutputWriter,
Doctrine\Dbal\Schema\Table,
Doctrine\Dbal\Schema\Column,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Column,
Doctrine\DBAL\Types\Type;

/**
Expand Down
141 changes: 122 additions & 19 deletions src/vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php
Expand Up @@ -38,8 +38,22 @@
*/
class DatabaseDriver implements Driver
{
/** The SchemaManager. */
/**
* @var AbstractSchemaManager
*/
private $_sm;

/**
* @var array
*/
private $tables = null;

private $classes = array();

/**
* @var array
*/
private $manyToManyTables = array();

/**
* Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
Expand All @@ -51,22 +65,70 @@ public function __construct(AbstractSchemaManager $schemaManager)
{
$this->_sm = $schemaManager;
}

private function reverseEngineerMappingFromDatabase()
{
if ($this->tables !== null) {
return;
}

foreach ($this->_sm->listTableNames() as $tableName) {
$tables[strtolower($tableName)] = $this->_sm->listTableDetails($tableName);
}

$this->tables = array();
foreach ($tables AS $name => $table) {
/* @var $table Table */
if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$foreignKeys = $table->getForeignKeys();
} else {
$foreignKeys = array();
}

$allForeignKeyColumns = array();
foreach ($foreignKeys AS $foreignKey) {
$allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
}

$pkColumns = $table->getPrimaryKey()->getColumns();
sort($pkColumns);
sort($allForeignKeyColumns);

if ($pkColumns == $allForeignKeyColumns) {
if (count($table->getForeignKeys()) > 2) {
throw new \InvalidArgumentException("ManyToMany table '" . $name . "' with more or less than two foreign keys are not supported by the Database Reverese Engineering Driver.");
}

$this->manyToManyTables[$name] = $table;
} else {
$className = Inflector::classify($name);
$this->tables[$name] = $table;
$this->classes[$className] = $name;
}
}
}

/**
* {@inheritdoc}
*/
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
{
$tableName = $className;
$className = Inflector::classify(strtolower($tableName));
$this->reverseEngineerMappingFromDatabase();

if (!isset($this->classes[$className])) {
throw new \InvalidArgumentException("Unknown class " . $className);
}

$tableName = Inflector::tableize($className);

$metadata->name = $className;
$metadata->table['name'] = $tableName;

$columns = $this->_sm->listTableColumns($tableName);
$columns = $this->tables[$tableName]->getColumns();
$indexes = $this->tables[$tableName]->getIndexes();

if ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$foreignKeys = $this->_sm->listTableForeignKeys($tableName);
$foreignKeys = $this->tables[$tableName]->getForeignKeys();
} else {
$foreignKeys = array();
}
Expand All @@ -76,8 +138,6 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
$allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
}

$indexes = $this->_sm->listTableIndexes($tableName);

$ids = array();
$fieldMappings = array();
foreach ($columns as $column) {
Expand Down Expand Up @@ -121,23 +181,71 @@ public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
$metadata->mapField($fieldMapping);
}

foreach ($this->manyToManyTables AS $manyTable) {
foreach ($manyTable->getForeignKeys() AS $foreignKey) {
if ($tableName == strtolower($foreignKey->getForeignTableName())) {
$myFk = $foreignKey;
foreach ($manyTable->getForeignKeys() AS $foreignKey) {
if ($foreignKey != $myFk) {
$otherFk = $foreignKey;
break;
}
}

$localColumn = current($myFk->getColumns());
$associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower(current($otherFk->getColumns()))));
$associationMapping['targetEntity'] = Inflector::classify(strtolower($otherFk->getForeignTableName()));
if (current($manyTable->getColumns())->getName() == $localColumn) {
$associationMapping['inversedBy'] = Inflector::camelize(str_replace('_id', '', strtolower(current($myFk->getColumns()))));
$associationMapping['joinTable'] = array(
'name' => strtolower($manyTable->getName()),
'joinColumns' => array(),
'inverseJoinColumns' => array(),
);

$fkCols = $myFk->getForeignColumns();
$cols = $myFk->getColumns();
for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinTable']['joinColumns'][] = array(
'name' => $cols[$i],
'referencedColumnName' => $fkCols[$i],
);
}

$fkCols = $otherFk->getForeignColumns();
$cols = $otherFk->getColumns();
for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinTable']['inverseJoinColumns'][] = array(
'name' => $cols[$i],
'referencedColumnName' => $fkCols[$i],
);
}
} else {
$associationMapping['mappedBy'] = Inflector::camelize(str_replace('_id', '', strtolower(current($myFk->getColumns()))));
}
$metadata->mapManyToMany($associationMapping);
break;
}
}
}

foreach ($foreignKeys as $foreignKey) {
$foreignTable = $foreignKey->getForeignTableName();
$cols = $foreignKey->getColumns();
$localColumn = current($cols);

$fkCols = $foreignKey->getForeignColumns();

$localColumn = current($cols);
$associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower($localColumn)));
$associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName());
$associationMapping['targetEntity'] = Inflector::classify($foreignTable);

for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinColumns'][] = array(
'name' => $cols[$i],
'referencedColumnName' => $fkCols[$i],
);
}

$metadata->mapManyToOne($associationMapping);
}
}
Expand All @@ -153,19 +261,14 @@ public function isTransient($className)
/**
* Return all the class names supported by this driver.
*
* IMPORTANT: This method must return an array of table names because we need
* to know the table name after we inflect it to create the entity class name.
* IMPORTANT: This method must return an array of class not tables names.
*
* @return array
*/
public function getAllClassNames()
{
$classes = array();

foreach ($this->_sm->listTableNames() as $tableName) {
$classes[] = $tableName;
}
$this->reverseEngineerMappingFromDatabase();

return $classes;
return array_keys($this->classes);
}
}
Expand Up @@ -25,7 +25,8 @@
Symfony\Components\Console\Input\InputOption,
Symfony\Components\Console,
Doctrine\ORM\Tools\Export\ClassMetadataExporter,
Doctrine\ORM\Tools\ConvertDoctrine1Schema;
Doctrine\ORM\Tools\ConvertDoctrine1Schema,
Doctrine\ORM\Tools\EntityGenerator;

/**
* Command to convert a Doctrine 1 schema to a Doctrine 2 mapping file.
Expand All @@ -41,6 +42,56 @@
*/
class ConvertDoctrine1SchemaCommand extends Console\Command\Command
{
/**
* @var EntityGenerator
*/
private $entityGenerator = null;

/**
* @var ClassMetadataExporter
*/
private $metadataExporter = null;

/**
* @return EntityGenerator
*/
public function getEntityGenerator()
{
if ($this->entityGenerator == null) {
$this->entityGenerator = new EntityGenerator();
}

return $this->entityGenerator;
}

/**
* @param EntityGenerator $entityGenerator
*/
public function setEntityGenerator(EntityGenerator $entityGenerator)
{
$this->entityGenerator = $entityGenerator;
}

/**
* @return ClassMetadataExporter
*/
public function getMetadataExporter()
{
if ($this->metadataExporter == null) {
$this->metadataExporter = new ClassMetadataExporter();
}

return $this->metadataExporter;
}

/**
* @param ClassMetadataExporter $metadataExporter
*/
public function setMetadataExporter(ClassMetadataExporter $metadataExporter)
{
$this->metadataExporter = $metadataExporter;
}

/**
* @see Console\Command\Command
*/
Expand Down Expand Up @@ -90,6 +141,27 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
// Process source directories
$fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from'));

// Process destination directory
$destPath = realpath($input->getArgument('dest-path'));

$toType = $input->getArgument('to-type');
$extend = $input->getOption('extend');
$numSpaces = $input->getOption('num-spaces');

$this->convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output);
}

/**
* @param \Doctrine\ORM\EntityManager $em
* @param array $fromPaths
* @param string $destPath
* @param string $toType
* @param int $numSpaces
* @param string|null $extend
* @param Console\Output\OutputInterface $output
*/
public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output)
{
foreach ($fromPaths as &$dirName) {
$dirName = realpath($dirName);

Expand All @@ -104,9 +176,6 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
}
}

// Process destination directory
$destPath = realpath($input->getArgument('dest-path'));

if ( ! file_exists($destPath)) {
throw new \InvalidArgumentException(
sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)
Expand All @@ -117,18 +186,16 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
);
}

$toType = $input->getArgument('to-type');

$cme = new ClassMetadataExporter();
$cme = $this->getMetadataExporter();
$exporter = $cme->getExporter($toType, $destPath);

if (strtolower($toType) === 'annotation') {
$entityGenerator = new EntityGenerator();
$entityGenerator = $this->getEntityGenerator();
$exporter->setEntityGenerator($entityGenerator);

$entityGenerator->setNumSpaces($input->getOption('num-spaces'));
$entityGenerator->setNumSpaces($numSpaces);

if (($extend = $input->getOption('extend')) !== null) {
if ($extend !== null) {
$entityGenerator->setClassToExtend($extend);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/vendor/propel/generator/default.properties
Expand Up @@ -16,7 +16,7 @@
#
# -------------------------------------------------------------------

propel.version = 1.5.2-dev
propel.version = 1.5.3-dev

propel.home = .

Expand Down Expand Up @@ -238,6 +238,17 @@ propel.platform.class = platform.${propel.database}Platform

propel.reverse.parser.class = reverse.${propel.database}.${propel.database}SchemaParser

# -------------------------------------------------------------------
#
# M Y S Q L S P E C I F I C S E T T I N G S
#
# -------------------------------------------------------------------

# Default table type
propel.mysql.tableType = MyISAM
# Keyword used to specify table type. MYSQL < 5 should use TYPE instead
propel.mysql.tableEngineKeyword = ENGINE

# -------------------------------------------------------------------
#
# B E H A V I O R S E T T I N G S
Expand Down

0 comments on commit 5994cb3

Please sign in to comment.