diff --git a/src/Mapping/ClassMetadata.php b/src/Mapping/ClassMetadata.php index 1de8dad5f9f..117bcb07989 100644 --- a/src/Mapping/ClassMetadata.php +++ b/src/Mapping/ClassMetadata.php @@ -47,6 +47,7 @@ * onDelete?: string, * columnDefinition?: string, * nullable?: bool, + * options?: array, * } * @psalm-type AssociationMapping = array{ * cache?: array, diff --git a/src/Tools/SchemaTool.php b/src/Tools/SchemaTool.php index 532d40d82a9..51709afd899 100644 --- a/src/Tools/SchemaTool.php +++ b/src/Tools/SchemaTool.php @@ -763,6 +763,10 @@ private function gatherRelationJoinColumns( if (isset($joinColumn['onDelete'])) { $fkOptions['onDelete'] = $joinColumn['onDelete']; } + + if (isset($joinColumn['options']['deferrable'])) { + $fkOptions['deferrable'] = $joinColumn['options']['deferrable']; + } } // Prefer unique constraints over implicit simple indexes created for foreign keys. diff --git a/tests/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php b/tests/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php index 2ccd2c20a11..3f3507e11e7 100644 --- a/tests/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php +++ b/tests/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php @@ -16,6 +16,7 @@ use Doctrine\Tests\OrmFunctionalTestCase; use function array_filter; +use function array_values; use function implode; use function str_starts_with; @@ -51,6 +52,20 @@ public function testUpdateSchemaWithPostgreSQLSchema(): void self::assertCount(0, $sql, implode("\n", $sql)); } + + public function testSetDeferrableForeignKey(): void + { + $schema = $this->getSchemaForModels( + EntityWithSelfReferencingAssociation::class + ); + + $table = $schema->getTable('postgres.entitywithselfreferencingassociation'); + $fks = array_values($table->getForeignKeys()); + + self::assertCount(1, $fks); + + self::assertTrue($fks[0]->getOption('deferrable')); + } } /** @@ -119,3 +134,26 @@ class DDC1657Avatar */ private $pk; } + +/** + * @Entity + */ +class EntityWithSelfReferencingAssociation +{ + /** + * Identifier + * + * @var int + * @Id + * @GeneratedValue(strategy="IDENTITY") + * @Column(type="integer", nullable=false) + */ + private $id; + + /** + * @var EntityWithSelfReferencingAssociation + * @ManyToOne(targetEntity="EntityWithSelfReferencingAssociation") + * @JoinColumn(options={"deferrable": true}) + */ + private $parent; +}