Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass join column options to FK #11499

Open
wants to merge 5 commits into
base: 3.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/en/reference/attributes-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ Optional parameters:
make foreign keys work.
- **options**:
See "options" attribute on :ref:`#[Column] <attrref_column>`.
It's possible to add other options, for example (PostgreSQL) `['deferrable' => true, 'deferred' => true]`.

Example:

Expand Down
5 changes: 5 additions & 0 deletions src/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use function array_filter;
use function array_flip;
use function array_intersect_key;
use function array_merge;
use function assert;
use function count;
use function current;
Expand Down Expand Up @@ -712,6 +713,10 @@ private function gatherRelationJoinColumns(
$uniqueConstraints[] = ['columns' => [$quotedColumnName]];
}

if ($joinColumn->options) {
$fkOptions = array_merge($fkOptions, $joinColumn->options);
}

if (isset($joinColumn->onDelete)) {
$fkOptions['onDelete'] = $joinColumn->onDelete;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Tests/ORM/Functional/SchemaTool/PostgreSqlSchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

use function array_filter;
use function array_slice;
use function implode;
use function str_starts_with;

Expand All @@ -42,6 +44,60 @@ public function testUpdateSchemaWithPostgreSQLSchema(): void

self::assertCount(0, $sql, implode("\n", $sql));
}

public function testUpdateSchemaWithJoinColumnWithOptions(): void
{
$sql = $this->getUpdateSchemaSqlForModels(
TestEntityWithJoinColumnWithOptions::class,
TestEntityWithJoinColumnWithOptionsRelation::class,
);

$this->assertSame([
'CREATE TABLE test (id INT NOT NULL, testRelation1_id INT DEFAULT NULL, testRelation2_id INT DEFAULT NULL, PRIMARY KEY(id))',
'CREATE UNIQUE INDEX UNIQ_D87F7E0C331521C6 ON test (testRelation1_id)',
'CREATE UNIQUE INDEX UNIQ_D87F7E0C21A08E28 ON test (testRelation2_id)',
'CREATE TABLE test_relation (id INT NOT NULL, PRIMARY KEY(id))',
'ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C331521C6 FOREIGN KEY (testRelation1_id) REFERENCES test_relation (id) DEFERRABLE INITIALLY DEFERRED',
'ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C21A08E28 FOREIGN KEY (testRelation2_id) REFERENCES test_relation (id) NOT DEFERRABLE INITIALLY IMMEDIATE',
], array_slice($sql, 0, 5));

foreach ($sql as $query) {
$this->_em->getConnection()->executeQuery($query);
}

$sql = $this->getUpdateSchemaSqlForModels(
TestEntityWithJoinColumnWithOptions::class,
TestEntityWithJoinColumnWithOptionsRelation::class,
);

$this->assertSame([], $sql);
}
}

#[Table('test')]
#[Entity]
class TestEntityWithJoinColumnWithOptions
{
#[Id]
#[Column]
private int $id;

#[OneToOne(targetEntity: TestEntityWithJoinColumnWithOptionsRelation::class)]
#[JoinColumn(options: ['deferrable' => true, 'deferred' => true])]
private TestEntityWithJoinColumnWithOptionsRelation $testRelation1;

#[OneToOne(targetEntity: TestEntityWithJoinColumnWithOptionsRelation::class)]
#[JoinColumn]
private TestEntityWithJoinColumnWithOptionsRelation $testRelation2;
}

#[Table('test_relation')]
#[Entity]
class TestEntityWithJoinColumnWithOptionsRelation
{
#[Id]
#[Column]
private int $id;
}

#[Table(name: 'stonewood.screen')]
Expand Down
48 changes: 48 additions & 0 deletions tests/Tests/ORM/Tools/SchemaToolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Tests\ORM\Tools;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
Expand Down Expand Up @@ -391,6 +392,53 @@ public function testLoadUniqueConstraintWithoutName(): void
self::assertTrue($tableIndex->isUnique());
self::assertSame(['field', 'anotherField'], $tableIndex->getColumns());
}

public function testJoinColumnWithOptions(): void
{
$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

$classes = [
$em->getClassMetadata(TestEntityWithJoinColumnWithOptions::class),
$em->getClassMetadata(TestEntityWithJoinColumnWithOptionsRelation::class),
];

$schema = $schemaTool->getSchemaFromMetadata($classes);

self::assertSame(['deferrable' => true, 'deferred' => true], $schema->getTable('test')->getForeignKey('FK_D87F7E0C331521C6')->getOptions());
self::assertSame([], $schema->getTable('test')->getForeignKey('FK_D87F7E0C21A08E28')->getOptions());

$sql = $schema->toSql(new PostgreSQLPlatform());

$this->assertSame('ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C331521C6 FOREIGN KEY (testRelation1_id) REFERENCES test_relation (id) DEFERRABLE INITIALLY DEFERRED', $sql[count($sql) - 2]);
$this->assertSame('ALTER TABLE test ADD CONSTRAINT FK_D87F7E0C21A08E28 FOREIGN KEY (testRelation2_id) REFERENCES test_relation (id) NOT DEFERRABLE INITIALLY IMMEDIATE', $sql[count($sql) - 1]);
}
}

#[Table('test')]
#[Entity]
class TestEntityWithJoinColumnWithOptions
{
#[Id]
#[Column]
private int $id;

#[OneToOne(targetEntity: TestEntityWithJoinColumnWithOptionsRelation::class)]
#[JoinColumn(options: ['deferrable' => true, 'deferred' => true])]
private TestEntityWithJoinColumnWithOptionsRelation $testRelation1;

#[OneToOne(targetEntity: TestEntityWithJoinColumnWithOptionsRelation::class)]
#[JoinColumn]
private TestEntityWithJoinColumnWithOptionsRelation $testRelation2;
}

#[Table('test_relation')]
#[Entity]
class TestEntityWithJoinColumnWithOptionsRelation
{
#[Id]
#[Column]
private int $id;
}

#[Table(options: ['foo' => 'bar', 'baz' => ['key' => 'val']])]
Expand Down