Skip to content

Commit 05f5486

Browse files
authored
Merge pull request #11549 from doctrine/feature/allow-overriding-cascade
Allow overriding association's cascade
2 parents 021a9cc + 129553d commit 05f5486

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Mapping/ClassMetadata.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ public function setInheritanceType(int $type): void
16871687
/**
16881688
* Sets the association to override association mapping of property for an entity relationship.
16891689
*
1690-
* @psalm-param array<string, mixed> $overrideMapping
1690+
* @psalm-param array{joinColumns?: array, inversedBy?: ?string, joinTable?: array, fetch?: ?string, cascade?: string[]} $overrideMapping
16911691
*
16921692
* @throws MappingException
16931693
*/
@@ -1723,6 +1723,10 @@ public function setAssociationOverride(string $fieldName, array $overrideMapping
17231723
$mapping['fetch'] = $overrideMapping['fetch'];
17241724
}
17251725

1726+
if (isset($overrideMapping['cascade'])) {
1727+
$mapping['cascade'] = $overrideMapping['cascade'];
1728+
}
1729+
17261730
switch ($mapping['type']) {
17271731
case self::ONE_TO_ONE:
17281732
case self::MANY_TO_ONE:

tests/Tests/ORM/Mapping/ClassMetadataTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,23 @@ public function testAssociationOverrideKeepsDeclaringClass(): void
872872
{
873873
$cm = new ClassMetadata(Directory::class);
874874
$cm->mapManyToOne(['fieldName' => 'parentDirectory', 'targetEntity' => Directory::class, 'cascade' => ['remove'], 'declared' => Directory::class]);
875-
$cm->setAssociationOverride('parentDirectory', ['cascade' => '']);
875+
$cm->setAssociationOverride('parentDirectory', ['cascade' => ['remove']]);
876876

877877
$mapping = $cm->getAssociationMapping('parentDirectory');
878878

879879
self::assertSame(Directory::class, $mapping->declared);
880880
}
881881

882+
public function testAssociationOverrideCanOverrideCascade(): void
883+
{
884+
$cm = new ClassMetadata(Directory::class);
885+
$cm->mapManyToOne(['fieldName' => 'parentDirectory', 'targetEntity' => Directory::class, 'cascade' => ['remove'], 'declared' => Directory::class]);
886+
$cm->setAssociationOverride('parentDirectory', ['cascade' => ['all']]);
887+
888+
$mapping = $cm->getAssociationMapping('parentDirectory');
889+
self::assertSame(['remove', 'persist', 'refresh', 'detach'], $mapping['cascade']);
890+
}
891+
882892
#[TestGroup('DDC-1955')]
883893
public function testInvalidEntityListenerClassException(): void
884894
{

0 commit comments

Comments
 (0)