set metadata for interface to be able to fetch entites by interface name#385
set metadata for interface to be able to fetch entites by interface name#385Burgov wants to merge 5 commits intodoctrine:masterfrom
Conversation
|
Hm, this only works if the CM for the actual entity is loaded before requesting the CM for the interface (see added failing test)... Any suggestions? |
|
hm this would be very cool, but i dont see a way to get it working. |
|
@beberlei perhaps a new preLoadClassMetadata event can be introduced. The ResolveTargetEntityListener could then listen into this event and make sure the original class is loaded, which in turn (if this PR would be merged) would make sure the interface metadata is registered as well |
|
@beberlei I've added a commit which makes it possble. Let me know what you think. |
|
Yes its better, but i fear you have to start over, we merged a huge refactoring in ClassMetadataFactory. But my idea would be to throw an event when a classmetadata could not be found, and then allow to recover from that event. |
There was a problem hiding this comment.
@beberlei this place is now in Common. If you want to trigger an event in this place, you will need to add a hook in Common.
There was a problem hiding this comment.
yes i know, thats what i mean with "start over" :-(
but the event should rather be "onClassMetadataNotFound" or something.
|
i am asking myself if the resolve metadata listener features should be in the core of metadata factory. Adding this event would only be for this use-case. I dont see any other use-case for the event. |
|
@beberlei What's your pov on this? Should it be in the core? Hopefully I will have time next week to rewrite the commit (or otherwise apply it to common). |
|
@beberlei I've rebased the branch on to master (and renamed the event as you suggested), but everything still works fine... Can you tell me what needs to change because of the refactoring? |
There was a problem hiding this comment.
you should tipehint the first argument if it is really a ClassMetadataInfo
|
@Burgov it looks like you messed the ClassMetadataFactory when rebasing, or you forgot to update your master before rebasing. Because currently, it is not based on the current code |
|
@stof you're right... It helps to rebase onto upstream/master instead of origin/master ^_^ I'll rewrite this PR later on |
|
you should add some tests |
|
Any help on this one please? In the original PR an event was dispatched at a place which has now been refactored to a base class in Doctrine/Common, which doesn't have the event dispatcher. I'm not sure how to resolve this...
|
|
Finally, Could you resolve this? Thanks. |
|
I ported this PR to #1181. Closing here. |
…licate imports (caused by rebase conflicts)
…ssed/cleared
…ewly introduced tests
…nterface-first fetching of metadata (via fallback logic)
…tadataNotFoundEventArgs`
…undEventArgs` from `ManagerEventArgs` instead of generic `EventArgs`
…etadataNotFoundEventArgs` API
…on to new `OnClassMetadataNotFoundEventArgs` API
…ataNotFoundEventArgs` impl
…EventArgs` docblocks as per @deeky666's review
…entities-by-aliased-name Support fetching entities by aliased name
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.
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.
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.
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.
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.
* Do not eagerly set metadata from ResolveTargetEntityListener 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 #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](#385 (comment)). 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. 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.
using the new ResolveTargetEntity functionality we noticed we needed another feature:
From the Symfony Bundle defining the interface, we'd like to be able to fetch entities by this very interface name, e.g.:
or
This PR sets metadata for the interface when metadata for a class is loaded that the interface is configured for