I would like to build an extensible Model for my application where 3rd party developers can dynamically add fields to my models without modifying my models itself. I think this can be accomplished by a “X has a Y” where the field X isn’t specified in the Entity Y. So 3rd party devs can just simply drop their entities in a folder and have Y discover it. Let me Give a more concrete example:
User
- Id
- Name
Lets say i want to add the collection Posts where user has many Posts. How can i do that without adding a Posts field to the user model. I’ve been reading about Unidirectional OneToMany etc but not sure if that would make my models easily extensible? How would you guys try to accomplish this?
The resolve target entity listener allows you to re-define associations at runtime. It allows you basically to map something like following:
As you can see, mapping an interface as target entity does not make much sense. It becomes really useful when you tell that every
My\Namespace\UserInterfacehas to be replaced with aOther\Namespace\Userreference.This would allow you other developers to replace the implementation of the associated objects, implementing only its interface and then setting a configuration key somewhere to override your mappings only where needed.
It works even without interfaces, but I strongly suggest to work with a clear API defined by a contract.