Skip to content

Do not eagerly set metadata from ResolveTargetEntityListener#12174

Merged
greg0ire merged 2 commits intodoctrine:2.20.xfrom
mpdude:resolve-target-entity-listener-no-eager-metadata
Oct 15, 2025
Merged

Do not eagerly set metadata from ResolveTargetEntityListener#12174
greg0ire merged 2 commits intodoctrine:2.20.xfrom
mpdude:resolve-target-entity-listener-no-eager-metadata

Conversation

@mpdude
Copy link
Copy Markdown
Contributor

@mpdude mpdude commented Sep 19, 2025

When using the ResolveTargetEntityListener to substitute targetEntities in association mappings, do not eagerly put the resolved (target) entity into the class metadata cache under the class name of the original entity.

Motivation

I have a library that wants to distribute a MappedSuperclass as the base for some functionality. It will be necessary that clients using the library will extend the MappedSuperclass to fill in some blanks, creating the first real #[Entity] instance of it.

This client-provided entity will be the primary means of working with the class. Thus, I was following the note in the documentation and using the ResolveTargetEntityListener to declare that whenever an association refers to that mapped superclass, the particular entity class shall be used instead.

One-To-Many associations are not generally possible on a mapped superclass, since they require the "many" side to hold the foreign key.
It is, however, possible to use the ResolveTargetEntityListener to replace references to a mapped superclass with an entity class at runtime. As long as there is only one entity subclass inheriting from the mapped superclass and all references to the mapped superclass are resolved to that entity class at runtime, the mapped superclass can use One-To-Many associations and be named as the targetEntity on the owning sides.

Changes made

The ResolveTargetEntityListener primarily does what its name suggests: For newly loaded class metadata, it inspects all associations declared and replaces the targetEntity with new (resolved) values.

But additionally, when a loaded class is the target of such a resolution, it would also put the class metadata into the cache under the name of the original entity.

I think that extra step is wrong, and this PR removes it. It had the side effect that when other classes extending the MappedSuperclass were loaded after the resolve target class has been seen for the first time, the metadata for those classes would not inherit from the mapped superclass anymore, but from the target entity class instead. In my real-life use case, this causes weird mapping errors down the road; as of ^3.0, it would throw a mapping exception asking to configure inheritance mapping. But note that there would be no inheritance between the entity classes at all.

More background

The documentation describes the use of ResolveTargetEntityListener with an interface that is resolved to an entity class. For an interface, adding the extra metadata does not make a difference, since it never interferes with actual entity or mapped superclasses.

The initial idea of adding a copy of the entity class metadata under the interface name came from commit
9c7f3f2 in #385. The goal was to make it possible to also find entities by interface names, like so:

$em->find('Foo\BarBundle\Entity\PersonInterface', 1);

It then turned out that this only worked when the resolution had already been applied. So, the new onClassMetadataNotFound event was added and the resolution map would be checked in that case as well (#1181). The inital code stayed in place, possibly giving a small performance gain.

@mpdude mpdude changed the base branch from 3.5.x to 2.20.x September 19, 2025 17:52
@mpdude mpdude requested a review from Copilot September 19, 2025 17:53

This comment was marked as resolved.

@mpdude mpdude changed the title Do not eagerly set metadata from ResolveTargetEntityListener Do not eagerly set metadata from ResolveTargetEntityListener Sep 19, 2025
@mpdude mpdude force-pushed the resolve-target-entity-listener-no-eager-metadata branch 4 times, most recently from 76ad0a7 to 45b801e Compare September 20, 2025 11:48
When using the `ResolveTargetEntityListener` to substitute `targetEntities` in association mappings, do not eagerly put the resolved (target) entity into the class metadata cache under the class name of the original entity.

#### Motivation

I have a library that wants to distribute a MappedSuperclass as the base for some functionality. It will be necessary that clients using the library will extend the MappedSuperclass to fill in some blanks, creating the first real `#[Entity]` instance of it.

This client-provided entity will be the primary means of working with the class. Thus, I was following the [note in the documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/3.5/reference/inheritance-mapping.html#mapped-superclasses:~:text=It%20is%2C%20however) and using the `ResolveTargetEntityListener` to declare that whenever an association refers to that mapped superclass, the particular entity class shall be used instead.

>  One-To-Many associations are not generally possible on a mapped superclass, since they require the "many" side to hold the foreign key.
> It is, however, possible to use the [ResolveTargetEntityListener](https://www.doctrine-project.org/projects/doctrine-orm/en/3.5/cookbook/resolve-target-entity-listener.html) to replace references to a mapped superclass with an entity class at runtime. As long as there is only one entity subclass inheriting from the mapped superclass and all references to the mapped superclass are resolved to that entity class at runtime, the mapped superclass can use One-To-Many associations and be named as the targetEntity on the owning sides.

#### Changes made

The `ResolveTargetEntityListener` primarily does what its name suggests: For newly loaded class metadata, it inspects all associations declared and replaces the `targetEntity` with new (resolved) values.

But additionally, when a loaded class is the target of such a resolution, it would also put the class metadata into the cache under the name of the original entity.

I think that extra step is wrong, and this PR removes it. It had the side effect that when other classes extending the MappedSuperclass were loaded _after_ the resolve target class has been seen for the first time, the metadata for those classes would not inherit from the mapped superclass anymore, but from the target entity class instead. In my real-life use case, this causes weird mapping errors down the road; as of ^3.0, it would throw a mapping exception asking to configure inheritance mapping. But note that there would be no inheritance between the two entity classes at all.

#### More background

The documentation [describes the use of `ResolveTargetEntityListener`](https://www.doctrine-project.org/projects/doctrine-orm/en/3.5/cookbook/resolve-target-entity-listener.html) with an interface that is resolved to an entity class. For an interface, adding the extra metadata does not make a difference, since it never interferes with actual entity or mapped superclasses.

The initial idea of adding a copy of the entity class metadata under the interface name came from commit
9c7f3f2 in doctrine#385. The goal was to make it possible to also find entities by interface names, like so:

```
$em->find('Foo\BarBundle\Entity\PersonInterface', 1);
```

It then [turned out that this only worked when the resolution had already been applied](doctrine#385 (comment)). So, the new `onClassMetadataNotFound` event was added and the resolution map would be checked in that case as well (doctrine#1181). The inital code stayed in place, possibly giving a small performance gain.

In my real-world use case and the test case I added in this PR, the associations are even self-referencing. That should not really be necessary for the problem to surface. I decided to keep it this way to show that the `targetEntity` need not be an interface after all, and that a MappedSuperclass can be used in the same way.
@mpdude mpdude force-pushed the resolve-target-entity-listener-no-eager-metadata branch from 45b801e to 8c6ae83 Compare September 22, 2025 07:37
@mpdude mpdude requested review from derrabus and greg0ire October 8, 2025 21:17
@greg0ire greg0ire added the Bug label Oct 8, 2025
@mpdude mpdude requested a review from beberlei October 11, 2025 14:09
Copy link
Copy Markdown
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

Can be smurfed

@greg0ire
Copy link
Copy Markdown
Member

Do you mean "squashed"! 🤣

@greg0ire greg0ire merged commit 9d680a6 into doctrine:2.20.x Oct 15, 2025
74 checks passed
@greg0ire
Copy link
Copy Markdown
Member

Smurfed. Thanks @mpdude !

@greg0ire greg0ire added this to the 2.20.7 milestone Oct 15, 2025
GromNaN added a commit to GromNaN/doctrine-persistence that referenced this pull request Oct 30, 2025
GromNaN added a commit to GromNaN/doctrine-persistence that referenced this pull request Oct 30, 2025
GromNaN added a commit to GromNaN/doctrine-persistence that referenced this pull request Oct 30, 2025
GromNaN added a commit to GromNaN/doctrine-persistence that referenced this pull request Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants