php - Symfony general entity relationship in Doctrine


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!




You could add 2 parameters to your PendingMessage Entity, oneentityName, the otherentityID.

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.




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);

People are also looking for solutions to the problem: image - Use Imagic methods in ImagicDraw in PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.