2009-04-03 15:06:58 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* 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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-04-03 15:06:58 +04:00
|
|
|
* <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,
|
2012-01-21 14:29:48 +04:00
|
|
|
Doctrine\DBAL\Schema\Schema,
|
|
|
|
Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets,
|
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.
|
|
|
|
*
|
2012-05-26 16:37:00 +04:00
|
|
|
*
|
2009-09-06 00:05:39 +04:00
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @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
|
|
|
|
*/
|
2012-06-04 21:26:05 +04:00
|
|
|
private $em;
|
2009-11-02 15:53:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Platforms\AbstractPlatform
|
|
|
|
*/
|
2012-06-04 21:26:05 +04:00
|
|
|
private $platform;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The quote strategy.
|
|
|
|
*
|
|
|
|
* @var \Doctrine\ORM\Mapping\QuoteStrategy
|
|
|
|
*/
|
2012-06-12 03:14:17 +04:00
|
|
|
private $quoteStrategy;
|
2009-04-03 15:06:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new SchemaTool instance that uses the connection of the
|
|
|
|
* provided EntityManager.
|
|
|
|
*
|
2011-12-12 00:52:29 +04:00
|
|
|
* @param \Doctrine\ORM\EntityManager $em
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
public function __construct(EntityManager $em)
|
|
|
|
{
|
2012-06-04 21:26:05 +04:00
|
|
|
$this->em = $em;
|
|
|
|
$this->platform = $em->getConnection()->getDatabasePlatform();
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the database schema for the given array of ClassMetadata instances.
|
|
|
|
*
|
2011-10-30 01:58:09 +04:00
|
|
|
* @throws ToolsException
|
2009-04-03 15:06:58 +04:00
|
|
|
* @param array $classes
|
2011-10-30 01:58:09 +04:00
|
|
|
* @return void
|
2009-04-03 15:06:58 +04:00
|
|
|
*/
|
|
|
|
public function createSchema(array $classes)
|
|
|
|
{
|
|
|
|
$createSchemaSql = $this->getCreateSchemaSql($classes);
|
2012-06-04 21:26:05 +04:00
|
|
|
$conn = $this->em->getConnection();
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
foreach ($createSchemaSql as $sql) {
|
2011-10-30 01:58:09 +04:00
|
|
|
try {
|
|
|
|
$conn->executeQuery($sql);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
throw ToolsException::schemaToolFailure($sql, $e);
|
|
|
|
}
|
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);
|
2012-06-04 21:26:05 +04:00
|
|
|
return $schema->toSql($this->platform);
|
2009-11-28 14:30:25 +03:00
|
|
|
}
|
|
|
|
|
2010-11-10 00:13:35 +03:00
|
|
|
/**
|
|
|
|
* Some instances of ClassMetadata don't need to be processed in the SchemaTool context. This method detects them.
|
2011-10-30 01:58:09 +04:00
|
|
|
*
|
2010-11-10 00:13:35 +03:00
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @param array $processedClasses
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function processingNotRequired($class, array $processedClasses)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
isset($processedClasses[$class->name]) ||
|
|
|
|
$class->isMappedSuperclass ||
|
|
|
|
($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:30:25 +03:00
|
|
|
/**
|
|
|
|
* From a given set of metadata classes this method creates a Schema instance.
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
public function getSchemaFromMetadata(array $classes)
|
|
|
|
{
|
2012-06-04 21:26:05 +04:00
|
|
|
// Reminder for processed classes, used for hierarchies
|
|
|
|
$processedClasses = array();
|
|
|
|
$eventManager = $this->em->getEventManager();
|
|
|
|
$schemaManager = $this->em->getConnection()->getSchemaManager();
|
|
|
|
$metadataSchemaConfig = $schemaManager->createSchemaConfig();
|
2009-04-03 15:06:58 +04:00
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
$metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
|
2012-01-21 14:29:48 +04:00
|
|
|
$schema = new Schema(array(), array(), $metadataSchemaConfig);
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2009-04-03 15:06:58 +04:00
|
|
|
foreach ($classes as $class) {
|
2010-11-10 00:13:35 +03:00
|
|
|
if ($this->processingNotRequired($class, $processedClasses)) {
|
2009-04-09 22:12:48 +04:00
|
|
|
continue;
|
2009-04-03 15:06:58 +04:00
|
|
|
}
|
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform));
|
2012-06-04 21:26:05 +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
|
2011-11-01 02:21:11 +04:00
|
|
|
$this->addDiscriminatorColumnDefinition($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) {
|
2012-06-04 21:26:05 +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'])) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$columnName = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $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) {
|
2011-11-01 02:21:11 +04:00
|
|
|
$this->addDiscriminatorColumnDefinition($class, $table);
|
2009-05-21 12:53:40 +04:00
|
|
|
} else {
|
|
|
|
// Add an ID FK column to child tables
|
2011-12-12 00:52:29 +04: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);
|
2012-06-12 03:14:17 +04:00
|
|
|
$columnName = $this->quoteStrategy->getColumnName($class->identifier[0], $class, $this->platform);
|
2010-11-19 01:14:07 +03:00
|
|
|
// TODO: This seems rather hackish, can we optimize it?
|
2011-01-23 18:12:26 +03:00
|
|
|
$table->getColumn($columnName)->setAutoincrement(false);
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2009-11-28 13:48:51 +03:00
|
|
|
$pkColumns[] = $columnName;
|
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(
|
2012-06-12 03:14:17 +04:00
|
|
|
$this->quoteStrategy->getTableName($this->em->getClassMetadata($class->rootEntityName), $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-08-07 21:33:54 +04:00
|
|
|
$pkColumns = array();
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($class->identifier as $identifierField) {
|
2010-08-07 21:33:54 +04:00
|
|
|
if (isset($class->fieldMappings[$identifierField])) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
|
2010-08-07 21:33:54 +04:00
|
|
|
} else if (isset($class->associationMappings[$identifierField])) {
|
|
|
|
/* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
|
|
|
|
$assoc = $class->associationMappings[$identifierField];
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($assoc['joinColumns'] as $joinColumn) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
2010-08-07 21:33:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 22:16:20 +04:00
|
|
|
|
2010-08-07 21:33:54 +04:00
|
|
|
if (!$table->hasIndex('primary')) {
|
|
|
|
$table->setPrimaryKey($pkColumns);
|
|
|
|
}
|
|
|
|
|
2010-03-29 17:20:41 +04:00
|
|
|
if (isset($class->table['indexes'])) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($class->table['indexes'] as $indexName => $indexData) {
|
2012-01-16 13:30:15 +04:00
|
|
|
$table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $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'])) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
|
2012-01-16 16:54:04 +04:00
|
|
|
$table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $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
|
|
|
|
2012-01-26 17:36:56 +04:00
|
|
|
if (isset($class->table['options'])) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($class->table['options'] as $key => $val) {
|
2012-01-26 17:36:56 +04:00
|
|
|
$table->addOption($key, $val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-06-06 00:10:44 +04:00
|
|
|
$seqDef = $class->sequenceGeneratorDefinition;
|
2012-06-12 03:14:17 +04:00
|
|
|
$quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform);
|
2012-06-06 00:10:44 +04:00
|
|
|
if ( ! $schema->hasSequence($quotedName)) {
|
2009-11-28 04:22:21 +03:00
|
|
|
$schema->createSequence(
|
2012-06-06 00:10:44 +04:00
|
|
|
$quotedName,
|
2009-11-28 04:22:21 +03:00
|
|
|
$seqDef['allocationSize'],
|
|
|
|
$seqDef['initialValue']
|
|
|
|
);
|
|
|
|
}
|
2009-05-05 21:20:55 +04:00
|
|
|
}
|
2010-03-19 01:36:27 +03:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) {
|
|
|
|
$eventManager->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
|
2010-03-19 01:36:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
if ( ! $this->platform->supportsSchemas() && ! $this->platform->canEmulateSchemas() ) {
|
2012-01-21 14:29:48 +04:00
|
|
|
$schema->visit(new RemoveNamespacedAssets());
|
|
|
|
}
|
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
|
|
|
|
$eventManager->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.
|
|
|
|
*/
|
2011-11-01 02:21:11 +04:00
|
|
|
private function addDiscriminatorColumnDefinition($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;
|
|
|
|
}
|
|
|
|
|
2012-02-14 05:22:49 +04:00
|
|
|
$options = array(
|
|
|
|
'length' => isset($discrColumn['length']) ? $discrColumn['length'] : null,
|
|
|
|
'notnull' => true
|
2009-11-28 04:22:21 +03:00
|
|
|
);
|
2012-02-14 05:22:49 +04:00
|
|
|
|
|
|
|
if (isset($discrColumn['columnDefinition'])) {
|
|
|
|
$options['columnDefinition'] = $discrColumn['columnDefinition'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$table->addColumn($discrColumn['name'], $discrColumn['type'], $options);
|
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) {
|
2011-01-02 12:18:02 +03:00
|
|
|
if ($class->isInheritanceTypeSingleTable() && isset($mapping['inherited'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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'])) {
|
2012-06-12 03:14:17 +04:00
|
|
|
$pkColumns[] = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $this->platform);
|
2009-11-28 04:22:21 +03:00
|
|
|
}
|
2009-04-09 22:12:48 +04:00
|
|
|
}
|
2010-08-07 21:33:54 +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')) {
|
2010-08-07 21:33:54 +04:00
|
|
|
//$table->setPrimaryKey($pkColumns);
|
2009-11-28 11:59:29 +03:00
|
|
|
}
|
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
|
|
|
{
|
2012-06-12 03:14:17 +04:00
|
|
|
$columnName = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $this->platform);
|
2009-11-28 13:48:51 +03:00
|
|
|
$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;
|
2010-09-28 01:22:52 +04:00
|
|
|
if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0) {
|
|
|
|
$options['notnull'] = false;
|
|
|
|
}
|
2009-11-28 13:48:51 +03:00
|
|
|
|
|
|
|
$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
|
|
|
|
2012-01-11 18:58:57 +04:00
|
|
|
if (isset($mapping['options'])) {
|
|
|
|
$options['customSchemaOptions'] = $mapping['options'];
|
|
|
|
}
|
|
|
|
|
2010-07-28 22:20:47 +04:00
|
|
|
if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == array($mapping['fieldName'])) {
|
|
|
|
$options['autoincrement'] = true;
|
|
|
|
}
|
2010-12-08 23:21:00 +03:00
|
|
|
if ($class->isInheritanceTypeJoined() && $class->name != $class->rootEntityName) {
|
|
|
|
$options['autoincrement'] = false;
|
|
|
|
}
|
2010-07-28 22:20:47 +04:00
|
|
|
|
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
|
|
|
|
2012-06-04 21:26:05 +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']) {
|
2010-08-11 00:07:43 +04:00
|
|
|
$primaryKeyColumns = $uniqueConstraints = array(); // PK is 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);
|
2010-08-11 00:07:43 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach($uniqueConstraints as $indexName => $unique) {
|
2010-08-11 00:07:43 +04:00
|
|
|
$table->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
|
|
|
|
}
|
2010-08-09 15:13:21 +04:00
|
|
|
} 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
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$theJoinTable = $schema->createTable($this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $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-08-11 00:07:43 +04:00
|
|
|
$theJoinTable->setPrimaryKey($primaryKeyColumns);
|
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach($uniqueConstraints as $indexName => $unique) {
|
2010-08-11 00:07:43 +04:00
|
|
|
$theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
|
2009-05-03 14:58:16 +04:00
|
|
|
}
|
2009-11-28 14:17:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-29 03:02:21 +03:00
|
|
|
/**
|
|
|
|
* Get the class metadata that is responsible for the definition of the referenced column name.
|
|
|
|
*
|
|
|
|
* Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
|
|
|
|
* not a simple field, go through all identifier field names that are associations recursivly and
|
|
|
|
* find that referenced column name.
|
|
|
|
*
|
|
|
|
* TODO: Is there any way to make this code more pleasing?
|
|
|
|
*
|
|
|
|
* @param ClassMetadata $class
|
|
|
|
* @param string $referencedColumnName
|
|
|
|
* @return array(ClassMetadata, referencedFieldName)
|
|
|
|
*/
|
|
|
|
private function getDefiningClass($class, $referencedColumnName)
|
|
|
|
{
|
|
|
|
$referencedFieldName = $class->getFieldName($referencedColumnName);
|
|
|
|
|
|
|
|
if ($class->hasField($referencedFieldName)) {
|
|
|
|
return array($class, $referencedFieldName);
|
|
|
|
} else if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
|
|
|
|
// it seems to be an entity as foreign key
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($class->getIdentifierFieldNames() as $fieldName) {
|
2011-01-01 20:17:19 +03:00
|
|
|
if ($class->hasAssociation($fieldName) && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
|
2010-12-29 03:02:21 +03:00
|
|
|
return $this->getDefiningClass(
|
2012-06-04 21:26:05 +04:00
|
|
|
$this->em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']),
|
2011-01-01 20:17:19 +03:00
|
|
|
$class->getSingleAssociationReferencedJoinColumnName($fieldName)
|
2010-12-29 03:02:21 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-11-28 14:17:31 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2010-12-29 03:02:21 +03:00
|
|
|
* @param array $mapping
|
2009-11-28 14:17:31 +03:00
|
|
|
* @param array $primaryKeyColumns
|
|
|
|
* @param array $uniqueConstraints
|
|
|
|
*/
|
|
|
|
private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
|
|
|
|
{
|
2012-06-04 21:26:05 +04:00
|
|
|
$localColumns = array();
|
|
|
|
$foreignColumns = array();
|
|
|
|
$fkOptions = array();
|
2012-06-12 03:14:17 +04:00
|
|
|
$foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform);
|
2009-11-28 14:17:31 +03:00
|
|
|
|
|
|
|
foreach ($joinColumns as $joinColumn) {
|
2012-06-06 20:14:20 +04:00
|
|
|
|
2012-06-11 21:21:07 +04:00
|
|
|
list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']);
|
2009-11-28 14:17:31 +03:00
|
|
|
|
2010-12-29 03:02:21 +03:00
|
|
|
if (!$definingClass) {
|
2010-03-05 19:35:00 +03:00
|
|
|
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
|
|
|
|
2012-06-12 03:14:17 +04:00
|
|
|
$quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
|
|
|
$quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $class, $this->platform);
|
2012-06-06 20:14:20 +04:00
|
|
|
|
|
|
|
$primaryKeyColumns[] = $quotedColumnName;
|
|
|
|
$localColumns[] = $quotedColumnName;
|
|
|
|
$foreignColumns[] = $quotedRefColumnName;
|
2009-11-28 04:22:21 +03:00
|
|
|
|
2012-06-06 20:14:20 +04:00
|
|
|
if ( ! $theJoinTable->hasColumn($quotedColumnName)) {
|
2009-12-07 19:21:29 +03:00
|
|
|
// 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
|
|
|
|
2010-12-29 03:02:21 +03:00
|
|
|
$fieldMapping = $definingClass->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'];
|
|
|
|
}
|
2011-01-13 23:43:33 +03:00
|
|
|
if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
|
2010-09-13 00:34:32 +04:00
|
|
|
$columnOptions['length'] = $fieldMapping['length'];
|
|
|
|
} else if ($fieldMapping['type'] == "decimal") {
|
|
|
|
$columnOptions['scale'] = $fieldMapping['scale'];
|
|
|
|
$columnOptions['precision'] = $fieldMapping['precision'];
|
|
|
|
}
|
2010-01-21 01:35:18 +03:00
|
|
|
|
2012-06-06 00:10:44 +04:00
|
|
|
$theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $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) {
|
2012-06-06 20:14:20 +04:00
|
|
|
$uniqueConstraints[] = array('columns' => array($quotedColumnName));
|
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['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(
|
2010-12-29 03:02:21 +03:00
|
|
|
$foreignTableName, $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-12-08 23:21:00 +03:00
|
|
|
$dropSchemaSql = $this->getDropSchemaSQL($classes);
|
2012-06-04 21:26:05 +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-12-23 00:04:11 +03:00
|
|
|
try {
|
|
|
|
$conn->executeQuery($sql);
|
|
|
|
} catch(\Exception $e) {
|
2011-10-30 01:58:09 +04:00
|
|
|
|
2010-12-23 00:04:11 +03:00
|
|
|
}
|
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
|
|
|
/**
|
2010-11-16 23:31:54 +03:00
|
|
|
* Drops all elements in the database of the current connection.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dropDatabase()
|
|
|
|
{
|
|
|
|
$dropSchemaSql = $this->getDropDatabaseSQL();
|
2012-06-04 21:26:05 +04:00
|
|
|
$conn = $this->em->getConnection();
|
2010-11-16 23:31:54 +03:00
|
|
|
|
|
|
|
foreach ($dropSchemaSql as $sql) {
|
|
|
|
$conn->executeQuery($sql);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SQL needed to drop the database schema for the connections database.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2010-11-16 23:31:54 +03:00
|
|
|
public function getDropDatabaseSQL()
|
2009-04-03 15:06:58 +04:00
|
|
|
{
|
2012-06-04 21:26:05 +04: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
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
$visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->platform);
|
2009-12-06 21:36:46 +03:00
|
|
|
/* @var $schema \Doctrine\DBAL\Schema\Schema */
|
|
|
|
$schema->visit($visitor);
|
|
|
|
return $visitor->getQueries();
|
2009-11-05 11:47:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-30 19:16:34 +04:00
|
|
|
* Get SQL to drop the tables defined by the passed classes.
|
2011-10-30 01:58:09 +04:00
|
|
|
*
|
2010-11-16 23:31:54 +03:00
|
|
|
* @param array $classes
|
2009-11-05 11:47:56 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
2010-11-16 23:31:54 +03:00
|
|
|
public function getDropSchemaSQL(array $classes)
|
2009-11-05 11:47:56 +03:00
|
|
|
{
|
2012-06-04 21:26:05 +04:00
|
|
|
$visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->platform);
|
2011-04-30 19:16:34 +04:00
|
|
|
$schema = $this->getSchemaFromMetadata($classes);
|
2011-04-26 19:32:04 +04:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
$sm = $this->em->getConnection()->getSchemaManager();
|
2011-04-30 19:16:34 +04:00
|
|
|
$fullSchema = $sm->createSchema();
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($fullSchema->getTables() as $table) {
|
2011-04-30 19:16:34 +04:00
|
|
|
if (!$schema->hasTable($table->getName())) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) {
|
2011-04-30 19:16:34 +04:00
|
|
|
/* @var $foreignKey \Doctrine\DBAL\Schema\ForeignKeyConstraint */
|
|
|
|
if ($schema->hasTable($foreignKey->getForeignTableName())) {
|
|
|
|
$visitor->acceptForeignKey($table, $foreignKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$visitor->acceptTable($table);
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($table->getForeignKeys() as $foreignKey) {
|
2011-04-30 19:16:34 +04:00
|
|
|
$visitor->acceptForeignKey($table, $foreignKey);
|
2011-04-26 19:32:04 +04:00
|
|
|
}
|
2010-11-16 23:31:54 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-30 01:58:09 +04:00
|
|
|
|
2012-06-04 21:26:05 +04:00
|
|
|
if ($this->platform->supportsSequences()) {
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($schema->getSequences() as $sequence) {
|
2011-06-17 00:34:04 +04:00
|
|
|
$visitor->acceptSequence($sequence);
|
|
|
|
}
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($schema->getTables() as $table) {
|
2011-06-17 00:34:04 +04:00
|
|
|
/* @var $sequence Table */
|
|
|
|
if ($table->hasPrimaryKey()) {
|
|
|
|
$columns = $table->getPrimaryKey()->getColumns();
|
|
|
|
if (count($columns) == 1) {
|
|
|
|
$checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
|
|
|
|
if ($fullSchema->hasSequence($checkSequence)) {
|
|
|
|
$visitor->acceptSequence($fullSchema->getSequence($checkSequence));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2011-04-30 19:16:34 +04:00
|
|
|
return $visitor->getQueries();
|
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
|
2011-04-18 06:39:59 +04:00
|
|
|
* instances to the current database schema that is inspected. If $saveMode is set
|
|
|
|
* to true the command is executed in the Database, else SQL is returned.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes
|
2011-04-18 06:39:59 +04:00
|
|
|
* @param boolean $saveMode
|
2009-08-21 22:13:22 +04:00
|
|
|
* @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);
|
2012-06-04 21:26:05 +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.
|
2011-10-30 01:58:09 +04:00
|
|
|
* If $saveMode is set to true the command is executed in the Database,
|
2011-04-18 06:39:59 +04:00
|
|
|
* else SQL is returned.
|
2010-03-05 19:35:00 +03:00
|
|
|
*
|
2009-08-21 22:13:22 +04:00
|
|
|
* @param array $classes The classes to consider.
|
2011-04-18 06:39:59 +04:00
|
|
|
* @param boolean $saveMode True for writing to DB, false for SQL string
|
2009-08-21 22:13:22 +04:00
|
|
|
* @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
|
|
|
{
|
2012-06-04 21:26:05 +04: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) {
|
2012-06-04 21:26:05 +04:00
|
|
|
return $schemaDiff->toSaveSql($this->platform);
|
2009-12-07 02:11:35 +03:00
|
|
|
} else {
|
2012-06-04 21:26:05 +04:00
|
|
|
return $schemaDiff->toSql($this->platform);
|
2009-12-07 02:11:35 +03:00
|
|
|
}
|
2009-11-28 13:48:51 +03:00
|
|
|
}
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|