2009-04-03 15:06:58 +04:00
|
|
|
<?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;
|
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
use Doctrine\ORM\ORMException,
|
|
|
|
Doctrine\DBAL\Types\Type,
|
2009-08-31 20:21:29 +04:00
|
|
|
Doctrine\ORM\EntityManager,
|
2010-08-09 15:13:21 +04:00
|
|
|
Doctrine\ORM\Mapping\ClassMetadata,
|
2010-03-19 01:36:27 +03:00
|
|
|
Doctrine\ORM\Internal\CommitOrderCalculator,
|
|
|
|
Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs,
|
|
|
|
Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
|
2009-04-03 15:06:58 +04:00
|
|
|
|
|
|
|
/**
|
2009-08-31 20:21:29 +04:00
|
|
|
* The SchemaTool is a tool to create/drop/update database schemas based on
|
2009-04-03 15:06:58 +04:00
|
|
|
* <tt>ClassMetadata</tt> class descriptors.
|
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* @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>
|
2009-11-01 01:23:36 +03:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
class SchemaTool
|
|
|
|
{
|
2009-11-02 15:53:05 +03:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\ORM\EntityManager
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
private $_em;
|
2009-11-02 15:53:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
|
|
|
*/
|
2009-04-03 15:06:58 +04:00
|
|
|
private $_platform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new SchemaTool instance that uses the connection of the
|
|
|
|
* provided EntityManager.
|
|
|
|
*
|
|
|
|
* @param Doctrine\ORM\EntityManager $em
|
|
|
|
*/
|
|
|
|
public function __construct(EntityManager $em)
|
|
|
|
{
|
|
|
|
$this->_em = $em;
|
|
|
|
$this->_platform = $em->getConnection()->getDatabasePlatform();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the database schema for the given array of ClassMetadata instances.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
*/
|
|
|
|
public function createSchema(array $classes)
|
|
|
|
{
|
|
|
|
$createSchemaSql = $this->getCreateSchemaSql($classes);
|
|
|
|
$conn = $this->_em->getConnection();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
foreach ($createSchemaSql as $sql) {
|
2010-04-01 01:13:34 +04:00
|
|
|
$conn->executeQuery($sql);
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-21 22:13:22 +04:00
|
|
|
* Gets the list of DDL statements that are required to create the database schema for
|
|
|
|
* the given list of ClassMetadata instances.
|
2009-04-03 15:06:58 +04:00
|
|
|
*
|
|
|
|
* @param array $classes
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return array $sql The SQL statements needed to create the schema for the classes.
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
public function getCreateSchemaSql(array $classes)
|
|
|
|
{
|
2009-11-28 14:30:25 +03:00
|
|
|
$schema = $this->getSchemaFromMetadata($classes);
|
|
|
|
return $schema->toSql($this->_platform);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* From a given set of metadata classes this method creates a Schema instance.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
public function getSchemaFromMetadata(array $classes)
|
|
|
|
{
|
2009-05-05 21:20:55 +04:00
|
|
|
$processedClasses = array(); // Reminder for processed classes, used for hierarchies
|
2009-04-03 15:06:58 +04:00
|
|
|
|
2009-12-11 02:55:47 +03:00
|
|
|
$sm = $this->_em->getConnection()->getSchemaManager();
|
2010-03-29 17:20:41 +04:00
|
|
|
$metadataSchemaConfig = $sm->createSchemaConfig();
|
|
|
|
$metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
|
2010-02-26 01:05:23 +03:00
|
|
|
$schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $metadataSchemaConfig);
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2010-03-19 01:36:27 +03:00
|
|
|
$evm = $this->_em->getEventManager();
|
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
foreach ($classes as $class) {
|
2009-09-29 19:54:16 +04:00
|
|
|
if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) {
|
2009-04-09 22:12:48 +04:00
|
|
|
continue;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
2009-11-28 04:22:21 +03:00
|
|
|
$table = $schema->createTable($class->getQuotedTableName($this->_platform));
|
|
|
|
|
2010-07-28 22:20:47 +04:00
|
|
|
// TODO: Remove
|
|
|
|
/**if ($class->isIdGeneratorIdentity()) {
|
2009-11-28 04:22:21 +03:00
|
|
|
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
|
|
|
|
} else if ($class->isIdGeneratorSequence()) {
|
|
|
|
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_SEQUENCE);
|
2010-07-28 22:20:47 +04:00
|
|
|
}*/
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
$columns = array(); // table columns
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable()) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$columns = $this->_gatherColumns($class, $table);
|
2009-11-28 17:30:08 +03:00
|
|
|
$this->_gatherRelationsSql($class, $table, $schema);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
// Add the discriminator column
|
2009-11-28 04:22:21 +03:00
|
|
|
$discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
|
2009-04-09 22:12:48 +04:00
|
|
|
|
|
|
|
// Aggregate all the information from all classes in the hierarchy
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->parentClasses as $parentClassName) {
|
2009-04-09 22:12:48 +04:00
|
|
|
// Parent class information is already contained in this class
|
|
|
|
$processedClasses[$parentClassName] = true;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->subClasses as $subClassName) {
|
2009-05-03 14:58:16 +04:00
|
|
|
$subClass = $this->_em->getClassMetadata($subClassName);
|
2009-11-28 13:48:51 +03:00
|
|
|
$this->_gatherColumns($subClass, $table);
|
|
|
|
$this->_gatherRelationsSql($subClass, $table, $schema);
|
2009-04-09 22:12:48 +04:00
|
|
|
$processedClasses[$subClassName] = true;
|
|
|
|
}
|
|
|
|
} else if ($class->isInheritanceTypeJoined()) {
|
2009-05-21 12:53:40 +04:00
|
|
|
// Add all non-inherited fields as columns
|
2009-11-28 04:22:21 +03:00
|
|
|
$pkColumns = array();
|
2009-05-21 12:53:40 +04:00
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if ( ! isset($mapping['inherited'])) {
|
2009-10-19 00:36:02 +04:00
|
|
|
$columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
|
2009-11-28 13:48:51 +03:00
|
|
|
$this->_gatherColumn($class, $mapping, $table);
|
2009-11-28 04:22:21 +03:00
|
|
|
|
|
|
|
if ($class->isIdentifier($fieldName)) {
|
|
|
|
$pkColumns[] = $columnName;
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
$this->_gatherRelationsSql($class, $table, $schema);
|
2009-05-21 12:53:40 +04:00
|
|
|
|
|
|
|
// Add the discriminator column only to the root table
|
|
|
|
if ($class->name == $class->rootEntityName) {
|
2009-11-28 04:22:21 +03:00
|
|
|
$discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
|
2009-05-21 12:53:40 +04:00
|
|
|
} else {
|
|
|
|
// Add an ID FK column to child tables
|
2009-11-28 13:48:51 +03:00
|
|
|
/* @var Doctrine\ORM\Mapping\ClassMetadata $class */
|
2009-05-21 12:53:40 +04:00
|
|
|
$idMapping = $class->fieldMappings[$class->identifier[0]];
|
2009-11-28 13:48:51 +03:00
|
|
|
$this->_gatherColumn($class, $idMapping, $table);
|
|
|
|
$columnName = $class->getQuotedColumnName($class->identifier[0], $this->_platform);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
$pkColumns[] = $columnName;
|
2010-07-28 22:20:47 +04:00
|
|
|
// TODO: REMOVE
|
|
|
|
/*if ($table->isIdGeneratorIdentity()) {
|
2009-11-28 04:22:21 +03:00
|
|
|
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_NONE);
|
2010-07-28 22:20:47 +04:00
|
|
|
}*/
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
// Add a FK constraint on the ID column
|
2009-12-02 21:52:21 +03:00
|
|
|
$table->addUnnamedForeignKeyConstraint(
|
2009-11-28 04:22:21 +03:00
|
|
|
$this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform),
|
2009-11-29 18:51:14 +03:00
|
|
|
array($columnName), array($columnName), array('onDelete' => 'CASCADE')
|
2009-11-28 04:22:21 +03:00
|
|
|
);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
|
|
|
$table->setPrimaryKey($pkColumns);
|
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
} else if ($class->isInheritanceTypeTablePerClass()) {
|
2010-03-05 19:35:00 +03:00
|
|
|
throw ORMException::notSupported();
|
2009-05-21 12:53:40 +04:00
|
|
|
} else {
|
2009-11-28 13:48:51 +03:00
|
|
|
$this->_gatherColumns($class, $table);
|
|
|
|
$this->_gatherRelationsSql($class, $table, $schema);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if (isset($class->table['indexes'])) {
|
|
|
|
foreach ($class->table['indexes'] AS $indexName => $indexData) {
|
2009-12-04 10:19:51 +03:00
|
|
|
$table->addIndex($indexData['columns'], $indexName);
|
2009-11-28 04:22:21 +03:00
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if (isset($class->table['uniqueConstraints'])) {
|
|
|
|
foreach ($class->table['uniqueConstraints'] AS $indexName => $indexData) {
|
2010-01-06 16:23:56 +03:00
|
|
|
$table->addUniqueIndex($indexData['columns'], $indexName);
|
2009-11-28 04:22:21 +03:00
|
|
|
}
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
$processedClasses[$class->name] = true;
|
2009-05-05 21:20:55 +04:00
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
|
2010-03-15 20:19:00 +03:00
|
|
|
$seqDef = $class->sequenceGeneratorDefinition;
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
if (!$schema->hasSequence($seqDef['sequenceName'])) {
|
2009-11-28 04:22:21 +03:00
|
|
|
$schema->createSequence(
|
|
|
|
$seqDef['sequenceName'],
|
|
|
|
$seqDef['allocationSize'],
|
|
|
|
$seqDef['initialValue']
|
|
|
|
);
|
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
}
|
2010-03-19 01:36:27 +03:00
|
|
|
|
|
|
|
if ($evm->hasListeners(ToolEvents::postGenerateSchemaTable)) {
|
|
|
|
$evm->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($evm->hasListeners(ToolEvents::postGenerateSchema)) {
|
|
|
|
$evm->dispatchEvent(ToolEvents::postGenerateSchema, new GenerateSchemaEventArgs($this->_em, $schema));
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-28 14:30:25 +03:00
|
|
|
return $schema;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gets a portable column definition as required by the DBAL for the discriminator
|
|
|
|
* column of a class.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @return array The portable column definition of the discriminator column as required by
|
|
|
|
* the DBAL.
|
|
|
|
*/
|
2009-11-28 04:22:21 +03:00
|
|
|
private function _getDiscriminatorColumnDefinition($class, $table)
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
2009-05-14 14:03:09 +04:00
|
|
|
$discrColumn = $class->discriminatorColumn;
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2010-02-07 15:36:30 +03:00
|
|
|
if (!isset($discrColumn['type']) || (strtolower($discrColumn['type']) == 'string' && $discrColumn['length'] === null)) {
|
|
|
|
$discrColumn['type'] = 'string';
|
|
|
|
$discrColumn['length'] = 255;
|
|
|
|
}
|
|
|
|
|
2010-02-26 00:51:30 +03:00
|
|
|
$table->addColumn(
|
2010-03-05 19:35:00 +03:00
|
|
|
$discrColumn['name'],
|
2009-11-28 04:22:21 +03:00
|
|
|
$discrColumn['type'],
|
|
|
|
array('length' => $discrColumn['length'], 'notnull' => true)
|
|
|
|
);
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
|
|
|
|
2009-05-21 12:53:40 +04:00
|
|
|
/**
|
2009-08-21 22:13:22 +04:00
|
|
|
* Gathers the column definitions as required by the DBAL of all field mappings
|
|
|
|
* found in the given class.
|
2009-05-21 12:53:40 +04:00
|
|
|
*
|
|
|
|
* @param ClassMetadata $class
|
2009-11-28 11:59:29 +03:00
|
|
|
* @param Table $table
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return array The list of portable column definitions as required by the DBAL.
|
2009-05-21 12:53:40 +04:00
|
|
|
*/
|
2009-11-28 13:48:51 +03:00
|
|
|
private function _gatherColumns($class, $table)
|
2009-04-09 22:12:48 +04:00
|
|
|
{
|
|
|
|
$columns = array();
|
2009-11-28 04:22:21 +03:00
|
|
|
$pkColumns = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-05-14 14:03:09 +04:00
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$column = $this->_gatherColumn($class, $mapping, $table);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-28 04:22:21 +03:00
|
|
|
if ($class->isIdentifier($mapping['fieldName'])) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$pkColumns[] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
|
2009-11-28 04:22:21 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2009-11-28 11:59:29 +03:00
|
|
|
// For now, this is a hack required for single table inheritence, since this method is called
|
|
|
|
// twice by single table inheritence relations
|
|
|
|
if(!$table->hasIndex('primary')) {
|
|
|
|
$table->setPrimaryKey($pkColumns);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-09 22:12:48 +04:00
|
|
|
return $columns;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Creates a column definition as required by the DBAL from an ORM field mapping definition.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param ClassMetadata $class The class that owns the field mapping.
|
|
|
|
* @param array $mapping The field mapping.
|
2009-11-28 11:59:29 +03:00
|
|
|
* @param Table $table
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return array The portable column definition as required by the DBAL.
|
|
|
|
*/
|
2009-11-28 13:48:51 +03:00
|
|
|
private function _gatherColumn($class, array $mapping, $table)
|
2009-05-21 12:53:40 +04:00
|
|
|
{
|
2009-11-28 13:48:51 +03:00
|
|
|
$columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
|
|
|
|
$columnType = $mapping['type'];
|
2009-09-05 23:44:26 +04:00
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
$options = array();
|
|
|
|
$options['length'] = isset($mapping['length']) ? $mapping['length'] : null;
|
|
|
|
$options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
|
|
|
|
|
|
|
|
$options['platformOptions'] = array();
|
|
|
|
$options['platformOptions']['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
|
|
|
|
|
|
|
|
if(strtolower($columnType) == 'string' && $options['length'] === null) {
|
|
|
|
$options['length'] = 255;
|
2009-11-02 15:53:05 +03:00
|
|
|
}
|
|
|
|
|
2009-09-05 00:31:11 +04:00
|
|
|
if (isset($mapping['precision'])) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$options['precision'] = $mapping['precision'];
|
2009-09-05 00:31:11 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-05 00:31:11 +04:00
|
|
|
if (isset($mapping['scale'])) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$options['scale'] = $mapping['scale'];
|
2009-09-05 00:31:11 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-07-17 22:13:03 +04:00
|
|
|
if (isset($mapping['default'])) {
|
2009-11-28 13:48:51 +03:00
|
|
|
$options['default'] = $mapping['default'];
|
2009-07-17 22:13:03 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2010-01-21 01:35:18 +03:00
|
|
|
if (isset($mapping['columnDefinition'])) {
|
|
|
|
$options['columnDefinition'] = $mapping['columnDefinition'];
|
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2010-07-28 22:20:47 +04:00
|
|
|
if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == array($mapping['fieldName'])) {
|
|
|
|
$options['autoincrement'] = true;
|
|
|
|
}
|
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
if ($table->hasColumn($columnName)) {
|
2010-03-15 20:19:00 +03:00
|
|
|
// required in some inheritance scenarios
|
2009-11-28 13:48:51 +03:00
|
|
|
$table->changeColumn($columnName, $options);
|
2009-11-28 11:59:29 +03:00
|
|
|
} else {
|
2010-02-26 00:51:30 +03:00
|
|
|
$table->addColumn($columnName, $columnType, $options);
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
2009-12-09 00:52:26 +03:00
|
|
|
|
|
|
|
$isUnique = isset($mapping['unique']) ? $mapping['unique'] : false;
|
|
|
|
if ($isUnique) {
|
|
|
|
$table->addUniqueIndex(array($columnName));
|
|
|
|
}
|
2009-05-21 12:53:40 +04:00
|
|
|
}
|
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gathers the SQL for properly setting up the relations of the given class.
|
|
|
|
* This includes the SQL for foreign key constraints and join tables.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param ClassMetadata $class
|
2010-02-07 15:36:30 +03:00
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
2009-11-28 13:48:51 +03:00
|
|
|
private function _gatherRelationsSql($class, $table, $schema)
|
2009-05-03 14:58:16 +04:00
|
|
|
{
|
2009-05-21 12:53:40 +04:00
|
|
|
foreach ($class->associationMappings as $fieldName => $mapping) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if (isset($mapping['inherited'])) {
|
2009-05-21 12:53:40 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-06-14 21:34:28 +04:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$foreignClass = $this->_em->getClassMetadata($mapping['targetEntity']);
|
2010-02-07 15:36:30 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) {
|
2009-11-28 14:17:31 +03:00
|
|
|
$primaryKeyColumns = $uniqueConstraints = array(); // unnecessary for this relation-type
|
2010-02-07 15:36:30 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$this->_gatherRelationJoinColumns($mapping['joinColumns'], $table, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
|
|
|
|
} else if ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) {
|
2009-05-03 14:58:16 +04:00
|
|
|
//... create join table, one-many through join table supported later
|
2010-03-05 19:35:00 +03:00
|
|
|
throw ORMException::notSupported();
|
2010-08-09 15:13:21 +04:00
|
|
|
} else if ($mapping['type'] == ClassMetadata::MANY_TO_MANY && $mapping['isOwningSide']) {
|
2009-05-03 14:58:16 +04:00
|
|
|
// create join table
|
2010-08-09 15:13:21 +04:00
|
|
|
$joinTable = $mapping['joinTable'];
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2010-08-09 15:13:21 +04:00
|
|
|
$theJoinTable = $schema->createTable($foreignClass->getQuotedJoinTableName($mapping, $this->_platform));
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2010-01-06 16:23:56 +03:00
|
|
|
$primaryKeyColumns = $uniqueConstraints = array();
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
// Build first FK constraint (relation table => source table)
|
|
|
|
$this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
// Build second FK constraint (relation table => target table)
|
|
|
|
$this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
|
2009-11-03 21:33:38 +03:00
|
|
|
|
2010-01-06 16:23:56 +03:00
|
|
|
foreach($uniqueConstraints AS $indexName => $unique) {
|
|
|
|
$theJoinTable->addUniqueIndex(
|
|
|
|
$unique['columns'], is_numeric($indexName) ? null : $indexName
|
|
|
|
);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
$theJoinTable->setPrimaryKey($primaryKeyColumns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gather columns and fk constraints that are required for one part of relationship.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-11-28 14:17:31 +03:00
|
|
|
* @param array $joinColumns
|
|
|
|
* @param \Doctrine\DBAL\Schema\Table $theJoinTable
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @param \Doctrine\ORM\Mapping\AssociationMapping $mapping
|
|
|
|
* @param array $primaryKeyColumns
|
|
|
|
* @param array $uniqueConstraints
|
|
|
|
*/
|
|
|
|
private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
|
|
|
|
{
|
|
|
|
$localColumns = array();
|
|
|
|
$foreignColumns = array();
|
|
|
|
$fkOptions = array();
|
|
|
|
|
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2010-03-05 19:35:00 +03:00
|
|
|
$columnName = $joinColumn['name'];
|
2010-01-21 01:35:18 +03:00
|
|
|
$referencedFieldName = $class->getFieldName($joinColumn['referencedColumnName']);
|
2009-11-28 14:17:31 +03:00
|
|
|
|
2010-03-05 19:35:00 +03:00
|
|
|
if ( ! $class->hasField($referencedFieldName)) {
|
|
|
|
throw new \Doctrine\ORM\ORMException(
|
2009-11-28 14:17:31 +03:00
|
|
|
"Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
|
2010-08-09 15:13:21 +04:00
|
|
|
$mapping['sourceEntity'] . " towards ". $mapping['targetEntity'] . " does not exist."
|
2009-11-28 04:22:21 +03:00
|
|
|
);
|
2009-11-28 14:17:31 +03:00
|
|
|
}
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
$primaryKeyColumns[] = $columnName;
|
|
|
|
$localColumns[] = $columnName;
|
|
|
|
$foreignColumns[] = $joinColumn['referencedColumnName'];
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-12-07 19:21:29 +03:00
|
|
|
if ( ! $theJoinTable->hasColumn($joinColumn['name'])) {
|
|
|
|
// Only add the column to the table if it does not exist already.
|
|
|
|
// It might exist already if the foreign key is mapped into a regular
|
|
|
|
// property as well.
|
2010-01-21 01:35:18 +03:00
|
|
|
|
|
|
|
$fieldMapping = $class->getFieldMapping($referencedFieldName);
|
2010-02-02 00:48:27 +03:00
|
|
|
|
|
|
|
$columnDef = null;
|
|
|
|
if (isset($joinColumn['columnDefinition'])) {
|
|
|
|
$columnDef = $joinColumn['columnDefinition'];
|
|
|
|
} else if (isset($fieldMapping['columnDefinition'])) {
|
|
|
|
$columnDef = $fieldMapping['columnDefinition'];
|
|
|
|
}
|
2010-01-21 01:35:18 +03:00
|
|
|
$columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
|
2010-02-07 15:36:30 +03:00
|
|
|
if (isset($joinColumn['nullable'])) {
|
|
|
|
$columnOptions['notnull'] = !$joinColumn['nullable'];
|
|
|
|
}
|
2010-01-21 01:35:18 +03:00
|
|
|
|
2010-02-26 00:51:30 +03:00
|
|
|
$theJoinTable->addColumn(
|
2010-01-21 01:35:18 +03:00
|
|
|
$columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), $columnOptions
|
2009-12-07 19:21:29 +03:00
|
|
|
);
|
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-12-07 19:21:29 +03:00
|
|
|
if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
|
2010-01-31 14:50:34 +03:00
|
|
|
$uniqueConstraints[] = array('columns' => array($columnName));
|
2009-11-28 14:17:31 +03:00
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
if (isset($joinColumn['onUpdate'])) {
|
|
|
|
$fkOptions['onUpdate'] = $joinColumn['onUpdate'];
|
|
|
|
}
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
if (isset($joinColumn['onDelete'])) {
|
|
|
|
$fkOptions['onDelete'] = $joinColumn['onDelete'];
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-28 14:17:31 +03:00
|
|
|
|
2009-12-02 21:52:21 +03:00
|
|
|
$theJoinTable->addUnnamedForeignKeyConstraint(
|
2009-11-29 18:51:14 +03:00
|
|
|
$class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, $fkOptions
|
2009-11-28 14:17:31 +03:00
|
|
|
);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Drops the database schema for the given classes.
|
2009-11-05 11:47:56 +03:00
|
|
|
*
|
|
|
|
* In any way when an exception is thrown it is supressed since drop was
|
|
|
|
* issued for all classes of the schema and some probably just don't exist.
|
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-08 07:47:42 +04:00
|
|
|
public function dropSchema(array $classes)
|
2009-04-03 15:06:58 +04:00
|
|
|
{
|
2010-04-08 07:47:42 +04:00
|
|
|
$dropSchemaSql = $this->getDropSchemaSql($classes);
|
2009-08-21 22:13:22 +04:00
|
|
|
$conn = $this->_em->getConnection();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($dropSchemaSql as $sql) {
|
2010-04-01 01:13:34 +04:00
|
|
|
$conn->executeQuery($sql);
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the SQL needed to drop the database schema for the given classes.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-12-06 21:36:46 +03:00
|
|
|
public function getDropSchemaSql(array $classes)
|
2009-04-03 15:06:58 +04:00
|
|
|
{
|
2009-11-05 11:47:56 +03:00
|
|
|
$sm = $this->_em->getConnection()->getSchemaManager();
|
2009-12-06 21:36:46 +03:00
|
|
|
$schema = $sm->createSchema();
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-12-06 21:36:46 +03:00
|
|
|
$visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->_platform);
|
|
|
|
/* @var $schema \Doctrine\DBAL\Schema\Schema */
|
|
|
|
$schema->visit($visitor);
|
|
|
|
return $visitor->getQueries();
|
2009-11-05 11:47:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop all tables of the database connection.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-11-05 11:47:56 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function _getDropSchemaTablesDatabaseMode($classes)
|
|
|
|
{
|
|
|
|
$conn = $this->_em->getConnection();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-05 11:47:56 +03:00
|
|
|
$sm = $conn->getSchemaManager();
|
|
|
|
/* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
|
|
|
|
|
|
|
|
$allTables = $sm->listTables();
|
|
|
|
|
|
|
|
$orderedTables = $this->_getDropSchemaTablesMetadataMode($classes);
|
|
|
|
foreach($allTables AS $tableName) {
|
|
|
|
if(!in_array($tableName, $orderedTables)) {
|
|
|
|
$orderedTables[] = $tableName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $orderedTables;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getDropSchemaTablesMetadataMode(array $classes)
|
|
|
|
{
|
|
|
|
$orderedTables = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
$commitOrder = $this->_getCommitOrder($classes);
|
2009-09-05 21:46:18 +04:00
|
|
|
$associationTables = $this->_getAssociationTables($commitOrder);
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-09-05 21:46:18 +04:00
|
|
|
// Drop association tables first
|
|
|
|
foreach ($associationTables as $associationTable) {
|
2009-11-05 11:47:56 +03:00
|
|
|
$orderedTables[] = $associationTable;
|
2009-09-05 21:46:18 +04:00
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
// Drop tables in reverse commit order
|
|
|
|
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
|
|
|
|
$class = $commitOrder[$i];
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-10-07 11:45:08 +04:00
|
|
|
if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
|
|
|
|
|| $class->isMappedSuperclass) {
|
2009-08-21 22:13:22 +04:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-05 11:47:56 +03:00
|
|
|
|
|
|
|
$orderedTables[] = $class->getTableName();
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
//TODO: Drop other schema elements, like sequences etc.
|
2009-11-05 11:47:56 +03:00
|
|
|
|
|
|
|
return $orderedTables;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Updates the database schema of the given classes by comparing the ClassMetadata
|
|
|
|
* instances to the current database schema that is inspected.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes
|
|
|
|
* @return void
|
|
|
|
*/
|
2009-12-07 02:11:35 +03:00
|
|
|
public function updateSchema(array $classes, $saveMode=false)
|
2009-08-17 21:58:16 +04:00
|
|
|
{
|
2009-12-07 02:11:35 +03:00
|
|
|
$updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode);
|
2009-08-21 22:13:22 +04:00
|
|
|
$conn = $this->_em->getConnection();
|
2009-12-06 21:36:46 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
foreach ($updateSchemaSql as $sql) {
|
2010-04-01 01:13:34 +04:00
|
|
|
$conn->executeQuery($sql);
|
2009-08-21 22:13:22 +04:00
|
|
|
}
|
2009-08-17 21:58:16 +04:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-21 22:13:22 +04:00
|
|
|
/**
|
|
|
|
* Gets the sequence of SQL statements that need to be performed in order
|
|
|
|
* to bring the given class mappings in-synch with the relational schema.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes The classes to consider.
|
|
|
|
* @return array The sequence of SQL statements.
|
|
|
|
*/
|
2009-12-07 02:11:35 +03:00
|
|
|
public function getUpdateSchemaSql(array $classes, $saveMode=false)
|
2009-08-17 21:58:16 +04:00
|
|
|
{
|
2009-12-06 15:23:13 +03:00
|
|
|
$sm = $this->_em->getConnection()->getSchemaManager();
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2009-12-06 15:23:13 +03:00
|
|
|
$fromSchema = $sm->createSchema();
|
|
|
|
$toSchema = $this->getSchemaFromMetadata($classes);
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2009-12-06 15:23:13 +03:00
|
|
|
$comparator = new \Doctrine\DBAL\Schema\Comparator();
|
|
|
|
$schemaDiff = $comparator->compare($fromSchema, $toSchema);
|
2009-11-28 13:48:51 +03:00
|
|
|
|
2009-12-07 02:11:35 +03:00
|
|
|
if ($saveMode) {
|
|
|
|
return $schemaDiff->toSaveSql($this->_platform);
|
|
|
|
} else {
|
|
|
|
return $schemaDiff->toSql($this->_platform);
|
|
|
|
}
|
2009-11-28 13:48:51 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
private function _getCommitOrder(array $classes)
|
|
|
|
{
|
|
|
|
$calc = new CommitOrderCalculator;
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
// Calculate dependencies
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$calc->addClass($class);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc->isOwningSide) {
|
2010-08-09 15:13:21 +04:00
|
|
|
$targetClass = $this->_em->getClassMetadata($assoc['targetEntity']);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
if ( ! $calc->hasClass($targetClass->name)) {
|
|
|
|
$calc->addClass($targetClass);
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
// add dependency ($targetClass before $class)
|
|
|
|
$calc->addDependency($targetClass, $class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $calc->getCommitOrder();
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-05 21:46:18 +04:00
|
|
|
private function _getAssociationTables(array $classes)
|
|
|
|
{
|
|
|
|
$associationTables = array();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-05 21:46:18 +04:00
|
|
|
foreach ($classes as $class) {
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
2010-08-09 15:13:21 +04:00
|
|
|
if ($assoc->isOwningSide && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
|
2009-09-05 21:46:18 +04:00
|
|
|
$associationTables[] = $assoc->joinTable['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-09-05 21:46:18 +04:00
|
|
|
return $associationTables;
|
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|