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

Allow overriding association's cascade #11549

Merged
merged 1 commit into from
Oct 22, 2024
Merged
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
6 changes: 5 additions & 1 deletion src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,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 @@ -1712,6 +1712,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 @@ -870,13 +870,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']]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change in the test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously cascade was ignored, now it's leading to an error as it's supposed to be an array of strings. I reckon this test was originally done to pass something and see if nothing (declaring class) breaks


$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
Loading