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

Disallow lifecycle callbacks on embedded classes #10778

Merged
merged 1 commit into from
Jun 22, 2023
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 3.0

## BC BREAK: Lifecycle callback mapping on embedded classes is now explicitly forbidden

Lifecycle callback mapping on embedded classes produced no effect, and is now
explicitly forbidden to point out mistakes.

## BC BREAK: The `NOTIFY` change tracking policy is removed

You should use `DEFERRED_EXPLICIT` instead.
Expand Down
8 changes: 1 addition & 7 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2044,13 +2044,7 @@ public function getLifecycleCallbacks(string $event): array
public function addLifecycleCallback(string $callback, string $event): void
{
if ($this->isEmbeddedClass) {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/8381',
'Registering lifecycle callback %s on Embedded class %s is not doing anything and will throw exception in 3.0',
$event,
$this->name,
);
throw MappingException::illegalLifecycleCallbackOnEmbeddedClass($callback, $this->name);
}

if (isset($this->lifecycleCallbacks[$event]) && in_array($callback, $this->lifecycleCallbacks[$event], true)) {
Expand Down
13 changes: 13 additions & 0 deletions lib/Doctrine/ORM/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,19 @@ public static function lifecycleCallbackMethodNotFound(string $className, string
return new self("Entity '" . $className . "' has no method '" . $methodName . "' to be registered as lifecycle callback.");
}

/** @param class-string $className */
public static function illegalLifecycleCallbackOnEmbeddedClass(string $event, string $className): self
{
return new self(sprintf(
<<<'EXCEPTION'
Context: Attempt to register lifecycle callback "%s" on embedded class "%s".
Problem: Registering lifecycle callbacks on embedded classes is not allowed.
EXCEPTION,
$event,
$className,
));
}

public static function entityListenerClassNotFound(string $listenerName, string $className): self
{
return new self(sprintf('Entity Listener "%s" declared on "%s" not found.', $listenerName, $className));
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,20 @@ public function testRejectsEmbeddableWithoutValidClassName(): void
'columnPrefix' => false,
]);
}

public function testItAddingLifecycleCallbackOnEmbeddedClassIsIllegal(): void
{
$metadata = new ClassMetadata(self::class);
$metadata->isEmbeddedClass = true;

$this->expectException(MappingException::class);
$this->expectExceptionMessage(<<<'EXCEPTION'
Context: Attempt to register lifecycle callback "foo" on embedded class "Doctrine\Tests\ORM\Mapping\ClassMetadataTest".
Problem: Registering lifecycle callbacks on embedded classes is not allowed.
EXCEPTION);

$metadata->addLifecycleCallback('foo', 'bar');
}
}

#[MappedSuperclass]
Expand Down