11
11
use Doctrine \DBAL \Schema \Exception \IndexDoesNotExist ;
12
12
use Doctrine \DBAL \Schema \Exception \IndexNameInvalid ;
13
13
use Doctrine \DBAL \Schema \Exception \InvalidTableName ;
14
+ use Doctrine \DBAL \Schema \Exception \PrimaryKeyAlreadyExists ;
14
15
use Doctrine \DBAL \Schema \Exception \UniqueConstraintDoesNotExist ;
15
16
use Doctrine \DBAL \Types \Type ;
16
17
use LogicException ;
17
18
18
19
use function array_merge ;
19
20
use function array_values ;
20
- use function in_array ;
21
21
use function preg_match ;
22
22
use function sprintf ;
23
23
use function strtolower ;
@@ -30,15 +30,20 @@ class Table extends AbstractAsset
30
30
/** @var Column[] */
31
31
protected array $ _columns = [];
32
32
33
- /** @var Index[] */
34
- private array $ implicitIndexes = [];
35
-
36
33
/** @var array<string, string> keys are new names, values are old names */
37
34
protected array $ renamedColumns = [];
38
35
39
36
/** @var Index[] */
40
37
protected array $ _indexes = [];
41
38
39
+ /**
40
+ * The keys of this array are the names of the indexes that were implicitly created as backing for foreign key
41
+ * constraints. The values are not used but must be non-null for {@link isset()} to work correctly.
42
+ *
43
+ * @var array<string,true>
44
+ */
45
+ private array $ implicitIndexNames = [];
46
+
42
47
protected ?string $ _primaryKeyName = null ;
43
48
44
49
/** @var UniqueConstraint[] */
@@ -606,36 +611,41 @@ protected function _addColumn(Column $column): void
606
611
/**
607
612
* Adds an index to the table.
608
613
*/
609
- protected function _addIndex (Index $ indexCandidate ): self
614
+ protected function _addIndex (Index $ index ): self
610
615
{
611
- $ indexName = $ indexCandidate ->getName ();
612
- $ indexName = $ this ->normalizeIdentifier ($ indexName );
613
- $ replacedImplicitIndexes = [];
616
+ $ indexName = $ this ->normalizeIdentifier ($ index ->getName ());
614
617
615
- foreach ($ this ->implicitIndexes as $ name => $ implicitIndex ) {
616
- if (! $ implicitIndex ->isFulfilledBy ($ indexCandidate ) || ! isset ($ this ->_indexes [$ name ])) {
618
+ $ replacedImplicitIndexNames = [];
619
+
620
+ foreach ($ this ->implicitIndexNames as $ implicitIndexName => $ _ ) {
621
+ if (! isset ($ this ->_indexes [$ implicitIndexName ])) {
622
+ continue ;
623
+ }
624
+
625
+ if (! $ this ->_indexes [$ implicitIndexName ]->isFulfilledBy ($ index )) {
617
626
continue ;
618
627
}
619
628
620
- $ replacedImplicitIndexes [ ] = $ name ;
629
+ $ replacedImplicitIndexNames [ $ implicitIndexName ] = true ;
621
630
}
622
631
623
- if (
624
- (isset ($ this ->_indexes [$ indexName ]) && ! in_array ($ indexName , $ replacedImplicitIndexes , true )) ||
625
- ($ this ->_primaryKeyName !== null && $ indexCandidate ->isPrimary ())
626
- ) {
632
+ if (isset ($ this ->_indexes [$ indexName ]) && ! isset ($ replacedImplicitIndexNames [$ indexName ])) {
627
633
throw IndexAlreadyExists::new ($ indexName , $ this ->_name );
628
634
}
629
635
630
- foreach ($ replacedImplicitIndexes as $ name ) {
631
- unset($ this ->_indexes [$ name ], $ this ->implicitIndexes [$ name ]);
636
+ if ($ this ->_primaryKeyName !== null && $ index ->isPrimary ()) {
637
+ throw PrimaryKeyAlreadyExists::new ($ this ->_name );
638
+ }
639
+
640
+ foreach ($ replacedImplicitIndexNames as $ name => $ _ ) {
641
+ unset($ this ->_indexes [$ name ], $ this ->implicitIndexNames [$ name ]);
632
642
}
633
643
634
- if ($ indexCandidate ->isPrimary ()) {
644
+ if ($ index ->isPrimary ()) {
635
645
$ this ->_primaryKeyName = $ indexName ;
636
646
}
637
647
638
- $ this ->_indexes [$ indexName ] = $ indexCandidate ;
648
+ $ this ->_indexes [$ indexName ] = $ index ;
639
649
640
650
return $ this ;
641
651
}
@@ -671,7 +681,7 @@ protected function _addUniqueConstraint(UniqueConstraint $constraint): self
671
681
}
672
682
}
673
683
674
- $ this ->implicitIndexes [$ this ->normalizeIdentifier ($ indexName )] = $ indexCandidate ;
684
+ $ this ->implicitIndexNames [$ this ->normalizeIdentifier ($ indexName )] = true ;
675
685
676
686
return $ this ;
677
687
}
@@ -709,7 +719,7 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): s
709
719
}
710
720
711
721
$ this ->_addIndex ($ indexCandidate );
712
- $ this ->implicitIndexes [$ this ->normalizeIdentifier ($ indexName )] = $ indexCandidate ;
722
+ $ this ->implicitIndexNames [$ this ->normalizeIdentifier ($ indexName )] = true ;
713
723
714
724
return $ this ;
715
725
}
0 commit comments