Reputation: 695
I need to implement a PendingMessage class to store notifications for some of the entities of the Symfony2 app. Sometimes a notification will be created for one entity and sometimes for another one (there are many entities).
Is there any way to do an ORM relation in this PendingMessage class to store one entity but not an specific entity type, just a general entity (class), in order to have an attribute called '$destination' which should be an entity type.
Should I implement an interface? Any help is welcome!
Upvotes: 0
Views: 64
Reputation: 3938
You can add field in PendingMessage entity in which You will store serialized entity for which this message was created.
Then if You would like to change this particular entity, You would do something like:
$pendingMessage = $this->getRepository('Bundle:PendingMessage')->find(1);
$detachedEntity = $pendingMessage->getDestination();
$entity = $em->merge($detachedEntity);
$entity->anyChangesYouWant();
Upvotes: 1
Reputation: 4766
You could add 2 parameters to your PendingMessage Entity, one entityName
, the other entityID
.
With those parameters, you could access the repository in the controller like
$em = $this->getDoctrine()->getManager();
$pendingMessage = $this->getRepository('youBundle:PendingMessage')->find(1234);
$targetEntity = $this->getRepository('yourBundle:'.$pendingMessage->getEntityName())->find($pendingMessage->getEntityID());
If you want to do the same operation with this PendingMessage for every Entity possible, I'd write an Interface that will be used by every Repository you'll use, to that it's guaranteed that this function is given in every Repository that you'll receive dynamically.
If this isn't what your looking for, please clarify your question.
Upvotes: 1