PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/lib/Doctrine/ORM/Tools/SchemaTool.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 729 lines | 427 code | 113 blank | 189 comment | 108 complexity | 016c4625b41c52e11d8d236853f14121 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\ORM\ORMException,
  21. Doctrine\DBAL\Types\Type,
  22. Doctrine\ORM\EntityManager,
  23. Doctrine\ORM\Mapping\ClassMetadata,
  24. Doctrine\ORM\Internal\CommitOrderCalculator,
  25. Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs,
  26. Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
  27. /**
  28. * The SchemaTool is a tool to create/drop/update database schemas based on
  29. * <tt>ClassMetadata</tt> class descriptors.
  30. *
  31. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  32. * @link www.doctrine-project.org
  33. * @since 2.0
  34. * @version $Revision$
  35. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  36. * @author Jonathan Wage <jonwage@gmail.com>
  37. * @author Roman Borschel <roman@code-factory.org>
  38. * @author Benjamin Eberlei <kontakt@beberlei.de>
  39. */
  40. class SchemaTool
  41. {
  42. /**
  43. * @var \Doctrine\ORM\EntityManager
  44. */
  45. private $_em;
  46. /**
  47. * @var \Doctrine\DBAL\Platforms\AbstractPlatform
  48. */
  49. private $_platform;
  50. /**
  51. * Initializes a new SchemaTool instance that uses the connection of the
  52. * provided EntityManager.
  53. *
  54. * @param Doctrine\ORM\EntityManager $em
  55. */
  56. public function __construct(EntityManager $em)
  57. {
  58. $this->_em = $em;
  59. $this->_platform = $em->getConnection()->getDatabasePlatform();
  60. }
  61. /**
  62. * Creates the database schema for the given array of ClassMetadata instances.
  63. *
  64. * @param array $classes
  65. */
  66. public function createSchema(array $classes)
  67. {
  68. $createSchemaSql = $this->getCreateSchemaSql($classes);
  69. $conn = $this->_em->getConnection();
  70. foreach ($createSchemaSql as $sql) {
  71. $conn->executeQuery($sql);
  72. }
  73. }
  74. /**
  75. * Gets the list of DDL statements that are required to create the database schema for
  76. * the given list of ClassMetadata instances.
  77. *
  78. * @param array $classes
  79. * @return array $sql The SQL statements needed to create the schema for the classes.
  80. */
  81. public function getCreateSchemaSql(array $classes)
  82. {
  83. $schema = $this->getSchemaFromMetadata($classes);
  84. return $schema->toSql($this->_platform);
  85. }
  86. /**
  87. * Some instances of ClassMetadata don't need to be processed in the SchemaTool context. This method detects them.
  88. *
  89. * @param ClassMetadata $class
  90. * @param array $processedClasses
  91. * @return bool
  92. */
  93. private function processingNotRequired($class, array $processedClasses)
  94. {
  95. return (
  96. isset($processedClasses[$class->name]) ||
  97. $class->isMappedSuperclass ||
  98. ($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
  99. );
  100. }
  101. /**
  102. * From a given set of metadata classes this method creates a Schema instance.
  103. *
  104. * @param array $classes
  105. * @return Schema
  106. */
  107. public function getSchemaFromMetadata(array $classes)
  108. {
  109. $processedClasses = array(); // Reminder for processed classes, used for hierarchies
  110. $sm = $this->_em->getConnection()->getSchemaManager();
  111. $metadataSchemaConfig = $sm->createSchemaConfig();
  112. $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
  113. $schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $metadataSchemaConfig);
  114. $evm = $this->_em->getEventManager();
  115. foreach ($classes as $class) {
  116. if ($this->processingNotRequired($class, $processedClasses)) {
  117. continue;
  118. }
  119. $table = $schema->createTable($class->getQuotedTableName($this->_platform));
  120. $columns = array(); // table columns
  121. if ($class->isInheritanceTypeSingleTable()) {
  122. $columns = $this->_gatherColumns($class, $table);
  123. $this->_gatherRelationsSql($class, $table, $schema);
  124. // Add the discriminator column
  125. $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
  126. // Aggregate all the information from all classes in the hierarchy
  127. foreach ($class->parentClasses as $parentClassName) {
  128. // Parent class information is already contained in this class
  129. $processedClasses[$parentClassName] = true;
  130. }
  131. foreach ($class->subClasses as $subClassName) {
  132. $subClass = $this->_em->getClassMetadata($subClassName);
  133. $this->_gatherColumns($subClass, $table);
  134. $this->_gatherRelationsSql($subClass, $table, $schema);
  135. $processedClasses[$subClassName] = true;
  136. }
  137. } else if ($class->isInheritanceTypeJoined()) {
  138. // Add all non-inherited fields as columns
  139. $pkColumns = array();
  140. foreach ($class->fieldMappings as $fieldName => $mapping) {
  141. if ( ! isset($mapping['inherited'])) {
  142. $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  143. $this->_gatherColumn($class, $mapping, $table);
  144. if ($class->isIdentifier($fieldName)) {
  145. $pkColumns[] = $columnName;
  146. }
  147. }
  148. }
  149. $this->_gatherRelationsSql($class, $table, $schema);
  150. // Add the discriminator column only to the root table
  151. if ($class->name == $class->rootEntityName) {
  152. $discrColumnDef = $this->_getDiscriminatorColumnDefinition($class, $table);
  153. } else {
  154. // Add an ID FK column to child tables
  155. /* @var Doctrine\ORM\Mapping\ClassMetadata $class */
  156. $idMapping = $class->fieldMappings[$class->identifier[0]];
  157. $this->_gatherColumn($class, $idMapping, $table);
  158. $columnName = $class->getQuotedColumnName($class->identifier[0], $this->_platform);
  159. // TODO: This seems rather hackish, can we optimize it?
  160. $table->getColumn($columnName)->setAutoincrement(false);
  161. $pkColumns[] = $columnName;
  162. // Add a FK constraint on the ID column
  163. $table->addUnnamedForeignKeyConstraint(
  164. $this->_em->getClassMetadata($class->rootEntityName)->getTableName(),
  165. array($columnName), array($columnName), array('onDelete' => 'CASCADE')
  166. );
  167. }
  168. $table->setPrimaryKey($pkColumns);
  169. } else if ($class->isInheritanceTypeTablePerClass()) {
  170. throw ORMException::notSupported();
  171. } else {
  172. $this->_gatherColumns($class, $table);
  173. $this->_gatherRelationsSql($class, $table, $schema);
  174. }
  175. $pkColumns = array();
  176. foreach ($class->identifier AS $identifierField) {
  177. if (isset($class->fieldMappings[$identifierField])) {
  178. $pkColumns[] = $class->getQuotedColumnName($identifierField, $this->_platform);
  179. } else if (isset($class->associationMappings[$identifierField])) {
  180. /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
  181. $assoc = $class->associationMappings[$identifierField];
  182. foreach ($assoc['joinColumns'] AS $joinColumn) {
  183. $pkColumns[] = $joinColumn['name'];
  184. }
  185. }
  186. }
  187. if (!$table->hasIndex('primary')) {
  188. $table->setPrimaryKey($pkColumns);
  189. }
  190. if (isset($class->table['indexes'])) {
  191. foreach ($class->table['indexes'] AS $indexName => $indexData) {
  192. $table->addIndex($indexData['columns'], $indexName);
  193. }
  194. }
  195. if (isset($class->table['uniqueConstraints'])) {
  196. foreach ($class->table['uniqueConstraints'] AS $indexName => $indexData) {
  197. $table->addUniqueIndex($indexData['columns'], $indexName);
  198. }
  199. }
  200. $processedClasses[$class->name] = true;
  201. if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
  202. $seqDef = $class->sequenceGeneratorDefinition;
  203. if (!$schema->hasSequence($seqDef['sequenceName'])) {
  204. $schema->createSequence(
  205. $seqDef['sequenceName'],
  206. $seqDef['allocationSize'],
  207. $seqDef['initialValue']
  208. );
  209. }
  210. }
  211. if ($evm->hasListeners(ToolEvents::postGenerateSchemaTable)) {
  212. $evm->dispatchEvent(ToolEvents::postGenerateSchemaTable, new GenerateSchemaTableEventArgs($class, $schema, $table));
  213. }
  214. }
  215. if ($evm->hasListeners(ToolEvents::postGenerateSchema)) {
  216. $evm->dispatchEvent(ToolEvents::postGenerateSchema, new GenerateSchemaEventArgs($this->_em, $schema));
  217. }
  218. return $schema;
  219. }
  220. /**
  221. * Gets a portable column definition as required by the DBAL for the discriminator
  222. * column of a class.
  223. *
  224. * @param ClassMetadata $class
  225. * @return array The portable column definition of the discriminator column as required by
  226. * the DBAL.
  227. */
  228. private function _getDiscriminatorColumnDefinition($class, $table)
  229. {
  230. $discrColumn = $class->discriminatorColumn;
  231. if (!isset($discrColumn['type']) || (strtolower($discrColumn['type']) == 'string' && $discrColumn['length'] === null)) {
  232. $discrColumn['type'] = 'string';
  233. $discrColumn['length'] = 255;
  234. }
  235. $table->addColumn(
  236. $discrColumn['name'],
  237. $discrColumn['type'],
  238. array('length' => $discrColumn['length'], 'notnull' => true)
  239. );
  240. }
  241. /**
  242. * Gathers the column definitions as required by the DBAL of all field mappings
  243. * found in the given class.
  244. *
  245. * @param ClassMetadata $class
  246. * @param Table $table
  247. * @return array The list of portable column definitions as required by the DBAL.
  248. */
  249. private function _gatherColumns($class, $table)
  250. {
  251. $columns = array();
  252. $pkColumns = array();
  253. foreach ($class->fieldMappings as $fieldName => $mapping) {
  254. if ($class->isInheritanceTypeSingleTable() && isset($mapping['inherited'])) {
  255. continue;
  256. }
  257. $column = $this->_gatherColumn($class, $mapping, $table);
  258. if ($class->isIdentifier($mapping['fieldName'])) {
  259. $pkColumns[] = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  260. }
  261. }
  262. // For now, this is a hack required for single table inheritence, since this method is called
  263. // twice by single table inheritence relations
  264. if(!$table->hasIndex('primary')) {
  265. //$table->setPrimaryKey($pkColumns);
  266. }
  267. return $columns;
  268. }
  269. /**
  270. * Creates a column definition as required by the DBAL from an ORM field mapping definition.
  271. *
  272. * @param ClassMetadata $class The class that owns the field mapping.
  273. * @param array $mapping The field mapping.
  274. * @param Table $table
  275. * @return array The portable column definition as required by the DBAL.
  276. */
  277. private function _gatherColumn($class, array $mapping, $table)
  278. {
  279. $columnName = $class->getQuotedColumnName($mapping['fieldName'], $this->_platform);
  280. $columnType = $mapping['type'];
  281. $options = array();
  282. $options['length'] = isset($mapping['length']) ? $mapping['length'] : null;
  283. $options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
  284. if ($class->isInheritanceTypeSingleTable() && count($class->parentClasses) > 0) {
  285. $options['notnull'] = false;
  286. }
  287. $options['platformOptions'] = array();
  288. $options['platformOptions']['version'] = $class->isVersioned && $class->versionField == $mapping['fieldName'] ? true : false;
  289. if(strtolower($columnType) == 'string' && $options['length'] === null) {
  290. $options['length'] = 255;
  291. }
  292. if (isset($mapping['precision'])) {
  293. $options['precision'] = $mapping['precision'];
  294. }
  295. if (isset($mapping['scale'])) {
  296. $options['scale'] = $mapping['scale'];
  297. }
  298. if (isset($mapping['default'])) {
  299. $options['default'] = $mapping['default'];
  300. }
  301. if (isset($mapping['columnDefinition'])) {
  302. $options['columnDefinition'] = $mapping['columnDefinition'];
  303. }
  304. if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == array($mapping['fieldName'])) {
  305. $options['autoincrement'] = true;
  306. }
  307. if ($class->isInheritanceTypeJoined() && $class->name != $class->rootEntityName) {
  308. $options['autoincrement'] = false;
  309. }
  310. if ($table->hasColumn($columnName)) {
  311. // required in some inheritance scenarios
  312. $table->changeColumn($columnName, $options);
  313. } else {
  314. $table->addColumn($columnName, $columnType, $options);
  315. }
  316. $isUnique = isset($mapping['unique']) ? $mapping['unique'] : false;
  317. if ($isUnique) {
  318. $table->addUniqueIndex(array($columnName));
  319. }
  320. }
  321. /**
  322. * Gathers the SQL for properly setting up the relations of the given class.
  323. * This includes the SQL for foreign key constraints and join tables.
  324. *
  325. * @param ClassMetadata $class
  326. * @param \Doctrine\DBAL\Schema\Table $table
  327. * @param \Doctrine\DBAL\Schema\Schema $schema
  328. * @return void
  329. */
  330. private function _gatherRelationsSql($class, $table, $schema)
  331. {
  332. foreach ($class->associationMappings as $fieldName => $mapping) {
  333. if (isset($mapping['inherited'])) {
  334. continue;
  335. }
  336. $foreignClass = $this->_em->getClassMetadata($mapping['targetEntity']);
  337. if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) {
  338. $primaryKeyColumns = $uniqueConstraints = array(); // PK is unnecessary for this relation-type
  339. $this->_gatherRelationJoinColumns($mapping['joinColumns'], $table, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
  340. foreach($uniqueConstraints AS $indexName => $unique) {
  341. $table->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
  342. }
  343. } else if ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) {
  344. //... create join table, one-many through join table supported later
  345. throw ORMException::notSupported();
  346. } else if ($mapping['type'] == ClassMetadata::MANY_TO_MANY && $mapping['isOwningSide']) {
  347. // create join table
  348. $joinTable = $mapping['joinTable'];
  349. $theJoinTable = $schema->createTable($foreignClass->getQuotedJoinTableName($mapping, $this->_platform));
  350. $primaryKeyColumns = $uniqueConstraints = array();
  351. // Build first FK constraint (relation table => source table)
  352. $this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
  353. // Build second FK constraint (relation table => target table)
  354. $this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
  355. $theJoinTable->setPrimaryKey($primaryKeyColumns);
  356. foreach($uniqueConstraints AS $indexName => $unique) {
  357. $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * Get the class metadata that is responsible for the definition of the referenced column name.
  364. *
  365. * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
  366. * not a simple field, go through all identifier field names that are associations recursivly and
  367. * find that referenced column name.
  368. *
  369. * TODO: Is there any way to make this code more pleasing?
  370. *
  371. * @param ClassMetadata $class
  372. * @param string $referencedColumnName
  373. * @return array(ClassMetadata, referencedFieldName)
  374. */
  375. private function getDefiningClass($class, $referencedColumnName)
  376. {
  377. $referencedFieldName = $class->getFieldName($referencedColumnName);
  378. if ($class->hasField($referencedFieldName)) {
  379. return array($class, $referencedFieldName);
  380. } else if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
  381. // it seems to be an entity as foreign key
  382. foreach ($class->getIdentifierFieldNames() AS $fieldName) {
  383. if ($class->hasAssociation($fieldName) && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
  384. return $this->getDefiningClass(
  385. $this->_em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']),
  386. $class->getSingleAssociationReferencedJoinColumnName($fieldName)
  387. );
  388. }
  389. }
  390. }
  391. return null;
  392. }
  393. /**
  394. * Gather columns and fk constraints that are required for one part of relationship.
  395. *
  396. * @param array $joinColumns
  397. * @param \Doctrine\DBAL\Schema\Table $theJoinTable
  398. * @param ClassMetadata $class
  399. * @param array $mapping
  400. * @param array $primaryKeyColumns
  401. * @param array $uniqueConstraints
  402. */
  403. private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
  404. {
  405. $localColumns = array();
  406. $foreignColumns = array();
  407. $fkOptions = array();
  408. $foreignTableName = $class->getTableName();
  409. foreach ($joinColumns as $joinColumn) {
  410. $columnName = $joinColumn['name'];
  411. list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']);
  412. if (!$definingClass) {
  413. throw new \Doctrine\ORM\ORMException(
  414. "Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
  415. $mapping['sourceEntity'] . " towards ". $mapping['targetEntity'] . " does not exist."
  416. );
  417. }
  418. $primaryKeyColumns[] = $columnName;
  419. $localColumns[] = $columnName;
  420. $foreignColumns[] = $joinColumn['referencedColumnName'];
  421. if ( ! $theJoinTable->hasColumn($joinColumn['name'])) {
  422. // Only add the column to the table if it does not exist already.
  423. // It might exist already if the foreign key is mapped into a regular
  424. // property as well.
  425. $fieldMapping = $definingClass->getFieldMapping($referencedFieldName);
  426. $columnDef = null;
  427. if (isset($joinColumn['columnDefinition'])) {
  428. $columnDef = $joinColumn['columnDefinition'];
  429. } else if (isset($fieldMapping['columnDefinition'])) {
  430. $columnDef = $fieldMapping['columnDefinition'];
  431. }
  432. $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef);
  433. if (isset($joinColumn['nullable'])) {
  434. $columnOptions['notnull'] = !$joinColumn['nullable'];
  435. }
  436. if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
  437. $columnOptions['length'] = $fieldMapping['length'];
  438. } else if ($fieldMapping['type'] == "decimal") {
  439. $columnOptions['scale'] = $fieldMapping['scale'];
  440. $columnOptions['precision'] = $fieldMapping['precision'];
  441. }
  442. $theJoinTable->addColumn($columnName, $fieldMapping['type'], $columnOptions);
  443. }
  444. if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
  445. $uniqueConstraints[] = array('columns' => array($columnName));
  446. }
  447. if (isset($joinColumn['onUpdate'])) {
  448. $fkOptions['onUpdate'] = $joinColumn['onUpdate'];
  449. }
  450. if (isset($joinColumn['onDelete'])) {
  451. $fkOptions['onDelete'] = $joinColumn['onDelete'];
  452. }
  453. }
  454. $theJoinTable->addUnnamedForeignKeyConstraint(
  455. $foreignTableName, $localColumns, $foreignColumns, $fkOptions
  456. );
  457. }
  458. /**
  459. * Drops the database schema for the given classes.
  460. *
  461. * In any way when an exception is thrown it is supressed since drop was
  462. * issued for all classes of the schema and some probably just don't exist.
  463. *
  464. * @param array $classes
  465. * @return void
  466. */
  467. public function dropSchema(array $classes)
  468. {
  469. $dropSchemaSql = $this->getDropSchemaSQL($classes);
  470. $conn = $this->_em->getConnection();
  471. foreach ($dropSchemaSql as $sql) {
  472. try {
  473. $conn->executeQuery($sql);
  474. } catch(\Exception $e) {
  475. }
  476. }
  477. }
  478. /**
  479. * Drops all elements in the database of the current connection.
  480. *
  481. * @return void
  482. */
  483. public function dropDatabase()
  484. {
  485. $dropSchemaSql = $this->getDropDatabaseSQL();
  486. $conn = $this->_em->getConnection();
  487. foreach ($dropSchemaSql as $sql) {
  488. $conn->executeQuery($sql);
  489. }
  490. }
  491. /**
  492. * Gets the SQL needed to drop the database schema for the connections database.
  493. *
  494. * @return array
  495. */
  496. public function getDropDatabaseSQL()
  497. {
  498. $sm = $this->_em->getConnection()->getSchemaManager();
  499. $schema = $sm->createSchema();
  500. $visitor = new \Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector($this->_platform);
  501. /* @var $schema \Doctrine\DBAL\Schema\Schema */
  502. $schema->visit($visitor);
  503. return $visitor->getQueries();
  504. }
  505. /**
  506. *
  507. * @param array $classes
  508. * @return array
  509. */
  510. public function getDropSchemaSQL(array $classes)
  511. {
  512. $sm = $this->_em->getConnection()->getSchemaManager();
  513. $sql = array();
  514. $orderedTables = array();
  515. foreach ($classes AS $class) {
  516. if ($class->isIdGeneratorSequence() && !$class->isMappedSuperclass && $class->name == $class->rootEntityName && $this->_platform->supportsSequences()) {
  517. $sql[] = $this->_platform->getDropSequenceSQL($class->sequenceGeneratorDefinition['sequenceName']);
  518. }
  519. }
  520. $commitOrder = $this->_getCommitOrder($classes);
  521. $associationTables = $this->_getAssociationTables($commitOrder);
  522. // Drop association tables first
  523. foreach ($associationTables as $associationTable) {
  524. if (!in_array($associationTable, $orderedTables)) {
  525. $orderedTables[] = $associationTable;
  526. }
  527. }
  528. // Drop tables in reverse commit order
  529. for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
  530. $class = $commitOrder[$i];
  531. if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
  532. || $class->isMappedSuperclass) {
  533. continue;
  534. }
  535. if (!in_array($class->getTableName(), $orderedTables)) {
  536. $orderedTables[] = $class->getTableName();
  537. }
  538. }
  539. $dropTablesSql = array();
  540. foreach ($orderedTables AS $tableName) {
  541. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  542. $foreignKeys = $sm->listTableForeignKeys($tableName);
  543. foreach ($foreignKeys AS $foreignKey) {
  544. $sql[] = $this->_platform->getDropForeignKeySQL($foreignKey, $tableName);
  545. }
  546. $dropTablesSql[] = $this->_platform->getDropTableSQL($tableName);
  547. }
  548. return array_merge($sql, $dropTablesSql);
  549. }
  550. /**
  551. * Updates the database schema of the given classes by comparing the ClassMetadata
  552. * ins$tableNametances to the current database schema that is inspected.
  553. *
  554. * @param array $classes
  555. * @return void
  556. */
  557. public function updateSchema(array $classes, $saveMode=false)
  558. {
  559. $updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode);
  560. $conn = $this->_em->getConnection();
  561. foreach ($updateSchemaSql as $sql) {
  562. $conn->executeQuery($sql);
  563. }
  564. }
  565. /**
  566. * Gets the sequence of SQL statements that need to be performed in order
  567. * to bring the given class mappings in-synch with the relational schema.
  568. *
  569. * @param array $classes The classes to consider.
  570. * @return array The sequence of SQL statements.
  571. */
  572. public function getUpdateSchemaSql(array $classes, $saveMode=false)
  573. {
  574. $sm = $this->_em->getConnection()->getSchemaManager();
  575. $fromSchema = $sm->createSchema();
  576. $toSchema = $this->getSchemaFromMetadata($classes);
  577. $comparator = new \Doctrine\DBAL\Schema\Comparator();
  578. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  579. if ($saveMode) {
  580. return $schemaDiff->toSaveSql($this->_platform);
  581. } else {
  582. return $schemaDiff->toSql($this->_platform);
  583. }
  584. }
  585. private function _getCommitOrder(array $classes)
  586. {
  587. $calc = new CommitOrderCalculator;
  588. // Calculate dependencies
  589. foreach ($classes as $class) {
  590. $calc->addClass($class);
  591. foreach ($class->associationMappings as $assoc) {
  592. if ($assoc['isOwningSide']) {
  593. $targetClass = $this->_em->getClassMetadata($assoc['targetEntity']);
  594. if ( ! $calc->hasClass($targetClass->name)) {
  595. $calc->addClass($targetClass);
  596. }
  597. // add dependency ($targetClass before $class)
  598. $calc->addDependency($targetClass, $class);
  599. }
  600. }
  601. }
  602. return $calc->getCommitOrder();
  603. }
  604. private function _getAssociationTables(array $classes)
  605. {
  606. $associationTables = array();
  607. foreach ($classes as $class) {
  608. foreach ($class->associationMappings as $assoc) {
  609. if ($assoc['isOwningSide'] && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
  610. $associationTables[] = $assoc['joinTable']['name'];
  611. }
  612. }
  613. }
  614. return $associationTables;
  615. }
  616. }