Skip to content

Commit

Permalink
Merge pull request #11549 from doctrine/feature/allow-overriding-cascade
Browse files Browse the repository at this point in the history
Allow overriding association's cascade
  • Loading branch information
greg0ire authored Oct 22, 2024
2 parents 021a9cc + 129553d commit 05f5486
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ public function setInheritanceType(int $type): void
/**
* Sets the association to override association mapping of property for an entity relationship.
*
* @psalm-param array<string, mixed> $overrideMapping
* @psalm-param array{joinColumns?: array, inversedBy?: ?string, joinTable?: array, fetch?: ?string, cascade?: string[]} $overrideMapping
*
* @throws MappingException
*/
Expand Down Expand Up @@ -1723,6 +1723,10 @@ public function setAssociationOverride(string $fieldName, array $overrideMapping
$mapping['fetch'] = $overrideMapping['fetch'];
}

if (isset($overrideMapping['cascade'])) {
$mapping['cascade'] = $overrideMapping['cascade'];
}

switch ($mapping['type']) {
case self::ONE_TO_ONE:
case self::MANY_TO_ONE:
Expand Down
12 changes: 11 additions & 1 deletion tests/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,13 +872,23 @@ public function testAssociationOverrideKeepsDeclaringClass(): void
{
$cm = new ClassMetadata(Directory::class);
$cm->mapManyToOne(['fieldName' => 'parentDirectory', 'targetEntity' => Directory::class, 'cascade' => ['remove'], 'declared' => Directory::class]);
$cm->setAssociationOverride('parentDirectory', ['cascade' => '']);
$cm->setAssociationOverride('parentDirectory', ['cascade' => ['remove']]);

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

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

public function testAssociationOverrideCanOverrideCascade(): void
{
$cm = new ClassMetadata(Directory::class);
$cm->mapManyToOne(['fieldName' => 'parentDirectory', 'targetEntity' => Directory::class, 'cascade' => ['remove'], 'declared' => Directory::class]);
$cm->setAssociationOverride('parentDirectory', ['cascade' => ['all']]);

$mapping = $cm->getAssociationMapping('parentDirectory');
self::assertSame(['remove', 'persist', 'refresh', 'detach'], $mapping['cascade']);
}

#[TestGroup('DDC-1955')]
public function testInvalidEntityListenerClassException(): void
{
Expand Down

0 comments on commit 05f5486

Please sign in to comment.