Failed Conditions
Push — master ( 6744b4...2b8acb )
by Marco
60:45 queued 60:36
created

lib/Doctrine/ORM/Tools/SchemaTool.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools;
21
22
use Doctrine\ORM\ORMException;
23
use Doctrine\DBAL\Schema\Comparator;
24
use Doctrine\DBAL\Schema\Index;
25
use Doctrine\DBAL\Schema\Schema;
26
use Doctrine\DBAL\Schema\Table;
27
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
28
use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets;
29
use Doctrine\ORM\EntityManagerInterface;
30
use Doctrine\ORM\Mapping\ClassMetadata;
31
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
32
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
33
34
/**
35
 * The SchemaTool is a tool to create/drop/update database schemas based on
36
 * <tt>ClassMetadata</tt> class descriptors.
37
 *
38
 * @link    www.doctrine-project.org
39
 * @since   2.0
40
 * @author  Guilherme Blanco <[email protected]>
41
 * @author  Jonathan Wage <[email protected]>
42
 * @author  Roman Borschel <[email protected]>
43
 * @author  Benjamin Eberlei <[email protected]>
44
 * @author  Stefano Rodriguez <[email protected]>
45
 */
46
class SchemaTool
47
{
48
    /**
49
     * @var \Doctrine\ORM\EntityManagerInterface
50
     */
51
    private $em;
52
53
    /**
54
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
55
     */
56
    private $platform;
57
58
    /**
59
     * The quote strategy.
60
     *
61
     * @var \Doctrine\ORM\Mapping\QuoteStrategy
62
     */
63
    private $quoteStrategy;
64
65
    /**
66
     * Initializes a new SchemaTool instance that uses the connection of the
67
     * provided EntityManager.
68
     *
69
     * @param \Doctrine\ORM\EntityManagerInterface $em
70
     */
71 1263
    public function __construct(EntityManagerInterface $em)
72
    {
73 1263
        $this->em               = $em;
74 1263
        $this->platform         = $em->getConnection()->getDatabasePlatform();
75 1263
        $this->quoteStrategy    = $em->getConfiguration()->getQuoteStrategy();
76 1263
    }
77
78
    /**
79
     * Creates the database schema for the given array of ClassMetadata instances.
80
     *
81
     * @param array $classes
82
     *
83
     * @return void
84
     *
85
     * @throws ToolsException
86
     */
87 281
    public function createSchema(array $classes)
88
    {
89 281
        $createSchemaSql = $this->getCreateSchemaSql($classes);
90 281
        $conn = $this->em->getConnection();
91
92 281
        foreach ($createSchemaSql as $sql) {
93
            try {
94 281
                $conn->executeQuery($sql);
95 95
            } catch (\Exception $e) {
96 281
                throw ToolsException::schemaToolFailure($sql, $e);
97
            }
98
        }
99 186
    }
100
101
    /**
102
     * Gets the list of DDL statements that are required to create the database schema for
103
     * the given list of ClassMetadata instances.
104
     *
105
     * @param array $classes
106
     *
107
     * @return array The SQL statements needed to create the schema for the classes.
108
     */
109 281
    public function getCreateSchemaSql(array $classes)
110
    {
111 281
        $schema = $this->getSchemaFromMetadata($classes);
112
113 281
        return $schema->toSql($this->platform);
114
    }
115
116
    /**
117
     * Detects instances of ClassMetadata that don't need to be processed in the SchemaTool context.
118
     *
119
     * @param ClassMetadata $class
120
     * @param array         $processedClasses
121
     *
122
     * @return bool
123
     */
124 290
    private function processingNotRequired($class, array $processedClasses)
125
    {
126
        return (
127 290
            isset($processedClasses[$class->name]) ||
128 290
            $class->isMappedSuperclass ||
129 290
            $class->isEmbeddedClass ||
130 290
            ($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
131
        );
132
    }
133
134
    /**
135
     * Creates a Schema instance from a given set of metadata classes.
136
     *
137
     * @param array $classes
138
     *
139
     * @return Schema
140
     *
141
     * @throws \Doctrine\ORM\ORMException
142
     */
143 290
    public function getSchemaFromMetadata(array $classes)
144
    {
145
        // Reminder for processed classes, used for hierarchies
146 290
        $processedClasses       = [];
147 290
        $eventManager           = $this->em->getEventManager();
148 290
        $schemaManager          = $this->em->getConnection()->getSchemaManager();
149 290
        $metadataSchemaConfig   = $schemaManager->createSchemaConfig();
150
151 290
        $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
152 290
        $schema = new Schema([], [], $metadataSchemaConfig);
153
154 290
        $addedFks = [];
155 290
        $blacklistedFks = [];
156
157 290
        foreach ($classes as $class) {
158
            /** @var \Doctrine\ORM\Mapping\ClassMetadata $class */
159 290
            if ($this->processingNotRequired($class, $processedClasses)) {
160 39
                continue;
161
            }
162
163 290
            $table = $schema->createTable($this->quoteStrategy->getTableName($class, $this->platform));
164
165 290
            if ($class->isInheritanceTypeSingleTable()) {
166 42
                $this->gatherColumns($class, $table);
167 42
                $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
168
169
                // Add the discriminator column
170 42
                $this->addDiscriminatorColumnDefinition($class, $table);
171
172
                // Aggregate all the information from all classes in the hierarchy
173 42
                foreach ($class->parentClasses as $parentClassName) {
174
                    // Parent class information is already contained in this class
175
                    $processedClasses[$parentClassName] = true;
176
                }
177
178 42
                foreach ($class->subClasses as $subClassName) {
179 40
                    $subClass = $this->em->getClassMetadata($subClassName);
180 40
                    $this->gatherColumns($subClass, $table);
181 40
                    $this->gatherRelationsSql($subClass, $table, $schema, $addedFks, $blacklistedFks);
182 42
                    $processedClasses[$subClassName] = true;
183
                }
184 286
            } elseif ($class->isInheritanceTypeJoined()) {
185
                // Add all non-inherited fields as columns
186 51
                $pkColumns = [];
187 51
                foreach ($class->fieldMappings as $fieldName => $mapping) {
188 51
                    if ( ! isset($mapping['inherited'])) {
189 51
                        $columnName = $this->quoteStrategy->getColumnName(
190 51
                            $mapping['fieldName'],
191 51
                            $class,
192 51
                            $this->platform
193
                        );
194 51
                        $this->gatherColumn($class, $mapping, $table);
195
196 51
                        if ($class->isIdentifier($fieldName)) {
197 51
                            $pkColumns[] = $columnName;
198
                        }
199
                    }
200
                }
201
202 51
                $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
203
204
                // Add the discriminator column only to the root table
205 51
                if ($class->name == $class->rootEntityName) {
206 51
                    $this->addDiscriminatorColumnDefinition($class, $table);
207
                } else {
208
                    // Add an ID FK column to child tables
209 50
                    $inheritedKeyColumns = [];
210 50
                    foreach ($class->identifier as $identifierField) {
211 50
                        $idMapping = $class->fieldMappings[$identifierField];
212 50
                        if (isset($idMapping['inherited'])) {
213 50
                            $this->gatherColumn($class, $idMapping, $table);
214 50
                            $columnName = $this->quoteStrategy->getColumnName(
215 50
                                $identifierField,
216 50
                                $class,
217 50
                                $this->platform
218
                            );
219
                            // TODO: This seems rather hackish, can we optimize it?
220 50
                            $table->getColumn($columnName)->setAutoincrement(false);
221
222 50
                            $pkColumns[] = $columnName;
223 50
                            $inheritedKeyColumns[] = $columnName;
224
                        }
225
                    }
226 50
                    if (!empty($inheritedKeyColumns)) {
227
                        // Add a FK constraint on the ID column
228 50
                        $table->addForeignKeyConstraint(
229 50
                            $this->quoteStrategy->getTableName(
230 50
                                $this->em->getClassMetadata($class->rootEntityName),
231 50
                                $this->platform
232
                            ),
233 50
                            $inheritedKeyColumns,
234 50
                            $inheritedKeyColumns,
235 50
                            ['onDelete' => 'CASCADE']
236
                        );
237
                    }
238
239
                }
240
241 51
                $table->setPrimaryKey($pkColumns);
242
243 271
            } elseif ($class->isInheritanceTypeTablePerClass()) {
244
                throw ORMException::notSupported();
245
            } else {
246 271
                $this->gatherColumns($class, $table);
247 271
                $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
248
            }
249
250 290
            $pkColumns = [];
251
252 290
            foreach ($class->identifier as $identifierField) {
253 290
                if (isset($class->fieldMappings[$identifierField])) {
254 290
                    $pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
255 29
                } elseif (isset($class->associationMappings[$identifierField])) {
256
                    /* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
257 29
                    $assoc = $class->associationMappings[$identifierField];
258
259 29
                    foreach ($assoc['joinColumns'] as $joinColumn) {
260 290
                        $pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
261
                    }
262
                }
263
            }
264
265 290
            if ( ! $table->hasIndex('primary')) {
266 278
                $table->setPrimaryKey($pkColumns);
267
            }
268
269
            // there can be unique indexes automatically created for join column
270
            // if join column is also primary key we should keep only primary key on this column
271
            // so, remove indexes overruled by primary key
272 290
            $primaryKey = $table->getIndex('primary');
273
274 290
            foreach ($table->getIndexes() as $idxKey => $existingIndex) {
275 290
                if ($primaryKey->overrules($existingIndex)) {
276 290
                    $table->dropIndex($idxKey);
277
                }
278
            }
279
280 290
            if (isset($class->table['indexes'])) {
281 1
                foreach ($class->table['indexes'] as $indexName => $indexData) {
282 1
                    if ( ! isset($indexData['flags'])) {
283 1
                        $indexData['flags'] = [];
284
                    }
285
286 1
                    $table->addIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, (array) $indexData['flags'], isset($indexData['options']) ? $indexData['options'] : []);
287
                }
288
            }
289
290 290
            if (isset($class->table['uniqueConstraints'])) {
291 4
                foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
292 4
                    $uniqIndex = new Index($indexName, $indexData['columns'], true, false, [], isset($indexData['options']) ? $indexData['options'] : []);
293
294 4
                    foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
295 4
                        if ($tableIndex->isFullfilledBy($uniqIndex)) {
296 3
                            $table->dropIndex($tableIndexName);
297 4
                            break;
298
                        }
299
                    }
300
301 4
                    $table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : []);
302
                }
303
            }
304
305 290
            if (isset($class->table['options'])) {
306 1
                foreach ($class->table['options'] as $key => $val) {
307 1
                    $table->addOption($key, $val);
308
                }
309
            }
310
311 290
            $processedClasses[$class->name] = true;
312
313 290
            if ($class->isIdGeneratorSequence() && $class->name == $class->rootEntityName) {
314
                $seqDef     = $class->sequenceGeneratorDefinition;
315
                $quotedName = $this->quoteStrategy->getSequenceName($seqDef, $class, $this->platform);
316
                if ( ! $schema->hasSequence($quotedName)) {
317
                    $schema->createSequence(
318
                        $quotedName,
319
                        $seqDef['allocationSize'],
320
                        $seqDef['initialValue']
321
                    );
322
                }
323
            }
324
325 290
            if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) {
326 1
                $eventManager->dispatchEvent(
327 1
                    ToolEvents::postGenerateSchemaTable,
328 290
                    new GenerateSchemaTableEventArgs($class, $schema, $table)
329
                );
330
            }
331
        }
332
333 290
        if ( ! $this->platform->supportsSchemas() && ! $this->platform->canEmulateSchemas() ) {
334 8
            $schema->visit(new RemoveNamespacedAssets());
335
        }
336
337 290
        if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
338 1
            $eventManager->dispatchEvent(
339 1
                ToolEvents::postGenerateSchema,
340 1
                new GenerateSchemaEventArgs($this->em, $schema)
341
            );
342
        }
343
344 290
        return $schema;
345
    }
346
347
    /**
348
     * Gets a portable column definition as required by the DBAL for the discriminator
349
     * column of a class.
350
     *
351
     * @param ClassMetadata $class
352
     * @param Table         $table
353
     *
354
     * @return void
355
     */
356 88
    private function addDiscriminatorColumnDefinition($class, Table $table)
357
    {
358 88
        $discrColumn = $class->discriminatorColumn;
359
360 88
        if ( ! isset($discrColumn['type']) ||
361 88
            (strtolower($discrColumn['type']) == 'string' && ! isset($discrColumn['length']))
362
        ) {
363 1
            $discrColumn['type'] = 'string';
364 1
            $discrColumn['length'] = 255;
365
        }
366
367
        $options = [
368 88
            'length'    => isset($discrColumn['length']) ? $discrColumn['length'] : null,
369
            'notnull'   => true
370
        ];
371
372 88
        if (isset($discrColumn['columnDefinition'])) {
373
            $options['columnDefinition'] = $discrColumn['columnDefinition'];
374
        }
375
376 88
        $table->addColumn($discrColumn['name'], $discrColumn['type'], $options);
377 88
    }
378
379
    /**
380
     * Gathers the column definitions as required by the DBAL of all field mappings
381
     * found in the given class.
382
     *
383
     * @param ClassMetadata $class
384
     * @param Table         $table
385
     *
386
     * @return void
387
     */
388 278
    private function gatherColumns($class, Table $table)
389
    {
390 278
        $pkColumns = [];
391
392 278
        foreach ($class->fieldMappings as $mapping) {
393 278
            if ($class->isInheritanceTypeSingleTable() && isset($mapping['inherited'])) {
394 40
                continue;
395
            }
396
397 278
            $this->gatherColumn($class, $mapping, $table);
398
399 278
            if ($class->isIdentifier($mapping['fieldName'])) {
400 278
                $pkColumns[] = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $this->platform);
401
            }
402
        }
403 278
    }
404
405
    /**
406
     * Creates a column definition as required by the DBAL from an ORM field mapping definition.
407
     *
408
     * @param ClassMetadata $class   The class that owns the field mapping.
409
     * @param array         $mapping The field mapping.
410
     * @param Table         $table
411
     *
412
     * @return void
413
     */
414 290
    private function gatherColumn($class, array $mapping, Table $table)
415
    {
416 290
        $columnName = $this->quoteStrategy->getColumnName($mapping['fieldName'], $class, $this->platform);
417 290
        $columnType = $mapping['type'];
418
419 290
        $options = [];
420 290
        $options['length'] = isset($mapping['length']) ? $mapping['length'] : null;
421 290
        $options['notnull'] = isset($mapping['nullable']) ? ! $mapping['nullable'] : true;
422 290
        if ($class->isInheritanceTypeSingleTable() && $class->parentClasses) {
423 7
            $options['notnull'] = false;
424
        }
425
426 290
        $options['platformOptions'] = [];
427 290
        $options['platformOptions']['version'] = $class->isVersioned && $class->versionField === $mapping['fieldName'];
428
429 290
        if (strtolower($columnType) === 'string' && null === $options['length']) {
430 155
            $options['length'] = 255;
431
        }
432
433 290
        if (isset($mapping['precision'])) {
434 289
            $options['precision'] = $mapping['precision'];
435
        }
436
437 290
        if (isset($mapping['scale'])) {
438 289
            $options['scale'] = $mapping['scale'];
439
        }
440
441 290
        if (isset($mapping['default'])) {
442 30
            $options['default'] = $mapping['default'];
443
        }
444
445 290
        if (isset($mapping['columnDefinition'])) {
446 1
            $options['columnDefinition'] = $mapping['columnDefinition'];
447
        }
448
449 290
        if (isset($mapping['options'])) {
450 3
            $knownOptions = ['comment', 'unsigned', 'fixed', 'default'];
451
452 3
            foreach ($knownOptions as $knownOption) {
453 3
                if (array_key_exists($knownOption, $mapping['options'])) {
454 2
                    $options[$knownOption] = $mapping['options'][$knownOption];
455
456 3
                    unset($mapping['options'][$knownOption]);
457
                }
458
            }
459
460 3
            $options['customSchemaOptions'] = $mapping['options'];
461
        }
462
463 290
        if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == [$mapping['fieldName']]) {
464 256
            $options['autoincrement'] = true;
465
        }
466 290
        if ($class->isInheritanceTypeJoined() && $class->name !== $class->rootEntityName) {
467 50
            $options['autoincrement'] = false;
468
        }
469
470 290
        if ($table->hasColumn($columnName)) {
471
            // required in some inheritance scenarios
472
            $table->changeColumn($columnName, $options);
473
        } else {
474 290
            $table->addColumn($columnName, $columnType, $options);
475
        }
476
477 290
        $isUnique = isset($mapping['unique']) ? $mapping['unique'] : false;
478 290
        if ($isUnique) {
479 17
            $table->addUniqueIndex([$columnName]);
480
        }
481 290
    }
482
483
    /**
484
     * Gathers the SQL for properly setting up the relations of the given class.
485
     * This includes the SQL for foreign key constraints and join tables.
486
     *
487
     * @param ClassMetadata $class
488
     * @param Table         $table
489
     * @param Schema        $schema
490
     * @param array         $addedFks
491
     * @param array         $blacklistedFks
492
     *
493
     * @return void
494
     *
495
     * @throws \Doctrine\ORM\ORMException
496
     */
497 290
    private function gatherRelationsSql($class, $table, $schema, &$addedFks, &$blacklistedFks)
498
    {
499 290
        foreach ($class->associationMappings as $mapping) {
500 207
            if (isset($mapping['inherited'])) {
501 27
                continue;
502
            }
503
504 207
            $foreignClass = $this->em->getClassMetadata($mapping['targetEntity']);
505
506 207
            if ($mapping['type'] & ClassMetadata::TO_ONE && $mapping['isOwningSide']) {
507 185
                $primaryKeyColumns = []; // PK is unnecessary for this relation-type
508
509 185
                $this->gatherRelationJoinColumns(
510 185
                    $mapping['joinColumns'],
511 185
                    $table,
512 185
                    $foreignClass,
513 185
                    $mapping,
514 185
                    $primaryKeyColumns,
515 185
                    $addedFks,
516 185
                    $blacklistedFks
517
                );
518 153
            } elseif ($mapping['type'] == ClassMetadata::ONE_TO_MANY && $mapping['isOwningSide']) {
519
                //... create join table, one-many through join table supported later
520
                throw ORMException::notSupported();
521 153
            } elseif ($mapping['type'] == ClassMetadata::MANY_TO_MANY && $mapping['isOwningSide']) {
522
                // create join table
523 52
                $joinTable = $mapping['joinTable'];
524
525 52
                $theJoinTable = $schema->createTable(
526 52
                    $this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform)
527
                );
528
529 52
                $primaryKeyColumns = [];
530
531
                // Build first FK constraint (relation table => source table)
532 52
                $this->gatherRelationJoinColumns(
533 52
                    $joinTable['joinColumns'],
534 52
                    $theJoinTable,
535 52
                    $class,
536 52
                    $mapping,
537 52
                    $primaryKeyColumns,
538 52
                    $addedFks,
539 52
                    $blacklistedFks
540
                );
541
542
                // Build second FK constraint (relation table => target table)
543 52
                $this->gatherRelationJoinColumns(
544 52
                    $joinTable['inverseJoinColumns'],
545 52
                    $theJoinTable,
546 52
                    $foreignClass,
547 52
                    $mapping,
548 52
                    $primaryKeyColumns,
549 52
                    $addedFks,
550 52
                    $blacklistedFks
551
                );
552
553 207
                $theJoinTable->setPrimaryKey($primaryKeyColumns);
554
            }
555
        }
556 290
    }
557
558
    /**
559
     * Gets the class metadata that is responsible for the definition of the referenced column name.
560
     *
561
     * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
562
     * not a simple field, go through all identifier field names that are associations recursively and
563
     * find that referenced column name.
564
     *
565
     * TODO: Is there any way to make this code more pleasing?
566
     *
567
     * @param ClassMetadata $class
568
     * @param string        $referencedColumnName
569
     *
570
     * @return array (ClassMetadata, referencedFieldName)
571
     */
572 207
    private function getDefiningClass($class, $referencedColumnName)
573
    {
574 207
        $referencedFieldName = $class->getFieldName($referencedColumnName);
575
576 207
        if ($class->hasField($referencedFieldName)) {
577 207
            return [$class, $referencedFieldName];
578
        }
579
580 9
        if (in_array($referencedColumnName, $class->getIdentifierColumnNames())) {
581
            // it seems to be an entity as foreign key
582 9
            foreach ($class->getIdentifierFieldNames() as $fieldName) {
583 9
                if ($class->hasAssociation($fieldName)
584 9
                    && $class->getSingleAssociationJoinColumnName($fieldName) == $referencedColumnName) {
585 9
                    return $this->getDefiningClass(
586 9
                        $this->em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']),
587 9
                        $class->getSingleAssociationReferencedJoinColumnName($fieldName)
588
                    );
589
                }
590
            }
591
        }
592
593
        return null;
594
    }
595
596
    /**
597
     * Gathers columns and fk constraints that are required for one part of relationship.
598
     *
599
     * @param array         $joinColumns
600
     * @param Table         $theJoinTable
601
     * @param ClassMetadata $class
602
     * @param array         $mapping
603
     * @param array         $primaryKeyColumns
604
     * @param array         $addedFks
605
     * @param array         $blacklistedFks
606
     *
607
     * @return void
608
     *
609
     * @throws \Doctrine\ORM\ORMException
610
     */
611 207
    private function gatherRelationJoinColumns(
612
        $joinColumns,
613
        $theJoinTable,
614
        $class,
615
        $mapping,
616
        &$primaryKeyColumns,
617
        &$addedFks,
618
        &$blacklistedFks
619
    )
620
    {
621 207
        $localColumns       = [];
622 207
        $foreignColumns     = [];
623 207
        $fkOptions          = [];
624 207
        $foreignTableName   = $this->quoteStrategy->getTableName($class, $this->platform);
625 207
        $uniqueConstraints  = [];
626
627 207
        foreach ($joinColumns as $joinColumn) {
628
629 207
            list($definingClass, $referencedFieldName) = $this->getDefiningClass(
630 207
                $class,
631 207
                $joinColumn['referencedColumnName']
632
            );
633
634 207
            if ( ! $definingClass) {
635
                throw new \Doctrine\ORM\ORMException(
636
                    "Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
637
                    $mapping['sourceEntity'] . " towards ". $mapping['targetEntity'] . " does not exist."
638
                );
639
            }
640
641 207
            $quotedColumnName       = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
642 207
            $quotedRefColumnName    = $this->quoteStrategy->getReferencedJoinColumnName(
643 207
                $joinColumn,
644 207
                $class,
645 207
                $this->platform
646
            );
647
648 207
            $primaryKeyColumns[]    = $quotedColumnName;
649 207
            $localColumns[]         = $quotedColumnName;
650 207
            $foreignColumns[]       = $quotedRefColumnName;
651
652 207
            if ( ! $theJoinTable->hasColumn($quotedColumnName)) {
653
                // Only add the column to the table if it does not exist already.
654
                // It might exist already if the foreign key is mapped into a regular
655
                // property as well.
656
657 204
                $fieldMapping = $definingClass->getFieldMapping($referencedFieldName);
658
659 204
                $columnDef = null;
660 204
                if (isset($joinColumn['columnDefinition'])) {
661
                    $columnDef = $joinColumn['columnDefinition'];
662 204
                } elseif (isset($fieldMapping['columnDefinition'])) {
663 1
                    $columnDef = $fieldMapping['columnDefinition'];
664
                }
665
666 204
                $columnOptions = ['notnull' => false, 'columnDefinition' => $columnDef];
667
668 204
                if (isset($joinColumn['nullable'])) {
669 130
                    $columnOptions['notnull'] = !$joinColumn['nullable'];
670
                }
671
672 204
                if (isset($fieldMapping['options'])) {
673
                    $columnOptions['options'] = $fieldMapping['options'];
674
                }
675
676 204
                if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
677 3
                    $columnOptions['length'] = $fieldMapping['length'];
678 204
                } elseif ($fieldMapping['type'] == "decimal") {
679
                    $columnOptions['scale'] = $fieldMapping['scale'];
680
                    $columnOptions['precision'] = $fieldMapping['precision'];
681
                }
682
683 204
                $theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $columnOptions);
684
            }
685
686 207
            if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
687 67
                $uniqueConstraints[] = ['columns' => [$quotedColumnName]];
688
            }
689
690 207
            if (isset($joinColumn['onDelete'])) {
691 207
                $fkOptions['onDelete'] = $joinColumn['onDelete'];
692
            }
693
        }
694
695
        // Prefer unique constraints over implicit simple indexes created for foreign keys.
696
        // Also avoids index duplication.
697 207
        foreach ($uniqueConstraints as $indexName => $unique) {
698 67
            $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
699
        }
700
701 207
        $compositeName = $theJoinTable->getName().'.'.implode('', $localColumns);
702 207
        if (isset($addedFks[$compositeName])
703 1
            && ($foreignTableName != $addedFks[$compositeName]['foreignTableName']
704 207
            || 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))
705
        ) {
706 1
            foreach ($theJoinTable->getForeignKeys() as $fkName => $key) {
707 1
                if (0 === count(array_diff($key->getLocalColumns(), $localColumns))
708 1
                    && (($key->getForeignTableName() != $foreignTableName)
709 1
                    || 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))
710
                ) {
711 1
                    $theJoinTable->removeForeignKey($fkName);
712 1
                    break;
713
                }
714
            }
715 1
            $blacklistedFks[$compositeName] = true;
716 207
        } elseif (!isset($blacklistedFks[$compositeName])) {
717 207
            $addedFks[$compositeName] = ['foreignTableName' => $foreignTableName, 'foreignColumns' => $foreignColumns];
718 207
            $theJoinTable->addUnnamedForeignKeyConstraint(
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated with message: Use {@link addForeignKeyConstraint}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
719 207
                $foreignTableName,
720 207
                $localColumns,
721 207
                $foreignColumns,
722 207
                $fkOptions
723
            );
724
        }
725 207
    }
726
727
    /**
728
     * Drops the database schema for the given classes.
729
     *
730
     * In any way when an exception is thrown it is suppressed since drop was
731
     * issued for all classes of the schema and some probably just don't exist.
732
     *
733
     * @param array $classes
734
     *
735
     * @return void
736
     */
737 5
    public function dropSchema(array $classes)
738
    {
739 5
        $dropSchemaSql = $this->getDropSchemaSQL($classes);
740 5
        $conn = $this->em->getConnection();
741
742 5
        foreach ($dropSchemaSql as $sql) {
743
            try {
744 5
                $conn->executeQuery($sql);
745 5
            } catch (\Exception $e) {
746
747
            }
748
        }
749 5
    }
750
751
    /**
752
     * Drops all elements in the database of the current connection.
753
     *
754
     * @return void
755
     */
756
    public function dropDatabase()
757
    {
758
        $dropSchemaSql = $this->getDropDatabaseSQL();
759
        $conn = $this->em->getConnection();
760
761
        foreach ($dropSchemaSql as $sql) {
762
            $conn->executeQuery($sql);
763
        }
764
    }
765
766
    /**
767
     * Gets the SQL needed to drop the database schema for the connections database.
768
     *
769
     * @return array
770
     */
771
    public function getDropDatabaseSQL()
772
    {
773
        $sm = $this->em->getConnection()->getSchemaManager();
774
        $schema = $sm->createSchema();
775
776
        $visitor = new DropSchemaSqlCollector($this->platform);
777
        $schema->visit($visitor);
778
779
        return $visitor->getQueries();
780
    }
781
782
    /**
783
     * Gets SQL to drop the tables defined by the passed classes.
784
     *
785
     * @param array $classes
786
     *
787
     * @return array
788
     */
789 5
    public function getDropSchemaSQL(array $classes)
790
    {
791 5
        $visitor = new DropSchemaSqlCollector($this->platform);
792 5
        $schema = $this->getSchemaFromMetadata($classes);
793
794 5
        $sm = $this->em->getConnection()->getSchemaManager();
795 5
        $fullSchema = $sm->createSchema();
796
797 5
        foreach ($fullSchema->getTables() as $table) {
798 5
            if ( ! $schema->hasTable($table->getName())) {
799 5
                foreach ($table->getForeignKeys() as $foreignKey) {
800
                    /* @var $foreignKey \Doctrine\DBAL\Schema\ForeignKeyConstraint */
801
                    if ($schema->hasTable($foreignKey->getForeignTableName())) {
802 5
                        $visitor->acceptForeignKey($table, $foreignKey);
803
                    }
804
                }
805
            } else {
806 5
                $visitor->acceptTable($table);
807 5
                foreach ($table->getForeignKeys() as $foreignKey) {
808 5
                    $visitor->acceptForeignKey($table, $foreignKey);
809
                }
810
            }
811
        }
812
813 5
        if ($this->platform->supportsSequences()) {
814
            foreach ($schema->getSequences() as $sequence) {
815
                $visitor->acceptSequence($sequence);
816
            }
817
818
            foreach ($schema->getTables() as $table) {
819
                /* @var $sequence Table */
820
                if ($table->hasPrimaryKey()) {
821
                    $columns = $table->getPrimaryKey()->getColumns();
822
                    if (count($columns) == 1) {
823
                        $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
824
                        if ($fullSchema->hasSequence($checkSequence)) {
825
                            $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
826
                        }
827
                    }
828
                }
829
            }
830
        }
831
832 5
        return $visitor->getQueries();
833
    }
834
835
    /**
836
     * Updates the database schema of the given classes by comparing the ClassMetadata
837
     * instances to the current database schema that is inspected.
838
     *
839
     * @param array   $classes
840
     * @param boolean $saveMode If TRUE, only performs a partial update
841
     *                          without dropping assets which are scheduled for deletion.
842
     *
843
     * @return void
844
     */
845
    public function updateSchema(array $classes, $saveMode = false)
846
    {
847
        $updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode);
848
        $conn = $this->em->getConnection();
849
850
        foreach ($updateSchemaSql as $sql) {
851
            $conn->executeQuery($sql);
852
        }
853
    }
854
855
    /**
856
     * Gets the sequence of SQL statements that need to be performed in order
857
     * to bring the given class mappings in-synch with the relational schema.
858
     *
859
     * @param array   $classes  The classes to consider.
860
     * @param boolean $saveMode If TRUE, only generates SQL for a partial update
861
     *                          that does not include SQL for dropping assets which are scheduled for deletion.
862
     *
863
     * @return array The sequence of SQL statements.
864
     */
865 1
    public function getUpdateSchemaSql(array $classes, $saveMode = false)
866
    {
867 1
        $sm = $this->em->getConnection()->getSchemaManager();
868
869 1
        $fromSchema = $sm->createSchema();
870 1
        $toSchema = $this->getSchemaFromMetadata($classes);
871
872 1
        $comparator = new Comparator();
873 1
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);
874
875 1
        if ($saveMode) {
876
            return $schemaDiff->toSaveSql($this->platform);
877
        }
878
879 1
        return $schemaDiff->toSql($this->platform);
880
    }
881
}
882