Skip to content

Commit 2e5040a

Browse files
authored
Added methods cascadeOnDelete(), restrictOnDelete(), noActionOnDelete(), restrictOnUpdate(), nullOnUpdate(), noActionOnUpdate() for ForeignKeyDefinition. (#7693)
1 parent dc6cfae commit 2e5040a

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Schema/ForeignKeyDefinition.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,59 @@ public function cascadeOnUpdate(): static
3232
return $this->onUpdate('cascade');
3333
}
3434

35+
/**
36+
* Indicate that updates should be restricted.
37+
*/
38+
public function restrictOnUpdate(): static
39+
{
40+
return $this->onUpdate('restrict');
41+
}
42+
43+
/**
44+
* Indicate that updates should set the foreign key value to null.
45+
*/
46+
public function nullOnUpdate(): static
47+
{
48+
return $this->onUpdate('set null');
49+
}
50+
51+
/**
52+
* Indicate that updates should have "no action".
53+
*/
54+
public function noActionOnUpdate(): static
55+
{
56+
return $this->onUpdate('no action');
57+
}
58+
59+
/**
60+
* Indicate that deletes should cascade.
61+
*/
62+
public function cascadeOnDelete(): static
63+
{
64+
return $this->onDelete('cascade');
65+
}
66+
67+
/**
68+
* Indicate that deletes should be restricted.
69+
*/
70+
public function restrictOnDelete(): static
71+
{
72+
return $this->onDelete('restrict');
73+
}
74+
3575
/**
3676
* Indicate that deletes should set the foreign key value to null.
3777
*/
3878
public function nullOnDelete(): static
3979
{
4080
return $this->onDelete('set null');
4181
}
82+
83+
/**
84+
* Indicate that deletes should have "no action".
85+
*/
86+
public function noActionOnDelete(): static
87+
{
88+
return $this->onDelete('no action');
89+
}
4290
}

tests/MySqlSchemaGrammarTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,66 @@ public function testAddingForeignKey()
546546
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`)', $statements[0]);
547547
}
548548

549+
public function testAddingForeignKeyWithCascadeOnDelete()
550+
{
551+
$blueprint = new Blueprint('users');
552+
$blueprint->foreign('foo_id')->references('id')->on('orders')->cascadeOnDelete();
553+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
554+
555+
$this->assertCount(1, $statements);
556+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on delete cascade', $statements[0]);
557+
}
558+
559+
public function testAddingForeignKeyWithRestrictOnDelete()
560+
{
561+
$blueprint = new Blueprint('users');
562+
$blueprint->foreign('foo_id')->references('id')->on('orders')->restrictOnDelete();
563+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
564+
565+
$this->assertCount(1, $statements);
566+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on delete restrict', $statements[0]);
567+
}
568+
569+
public function testAddingForeignKeyWithNoActionOnDelete()
570+
{
571+
$blueprint = new Blueprint('users');
572+
$blueprint->foreign('foo_id')->references('id')->on('orders')->noActionOnDelete();
573+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
574+
575+
$this->assertCount(1, $statements);
576+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on delete no action', $statements[0]);
577+
}
578+
579+
public function testAddingForeignKeyWithRestrictOnUpdate()
580+
{
581+
$blueprint = new Blueprint('users');
582+
$blueprint->foreign('foo_id')->references('id')->on('orders')->restrictOnUpdate();
583+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
584+
585+
$this->assertCount(1, $statements);
586+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on update restrict', $statements[0]);
587+
}
588+
589+
public function testAddingForeignKeyWithNullOnUpdate()
590+
{
591+
$blueprint = new Blueprint('users');
592+
$blueprint->foreign('foo_id')->references('id')->on('orders')->nullOnUpdate();
593+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
594+
595+
$this->assertCount(1, $statements);
596+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on update set null', $statements[0]);
597+
}
598+
599+
public function testAddingForeignKeyWithNoActionOnUpdate()
600+
{
601+
$blueprint = new Blueprint('users');
602+
$blueprint->foreign('foo_id')->references('id')->on('orders')->noActionOnUpdate();
603+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
604+
605+
$this->assertCount(1, $statements);
606+
$this->assertSame('alter table `users` add constraint `users_foo_id_foreign` foreign key (`foo_id`) references `orders` (`id`) on update no action', $statements[0]);
607+
}
608+
549609
public function testAddingIncrementingID()
550610
{
551611
$blueprint = new Blueprint('users');

0 commit comments

Comments
 (0)