orm - Doctrine: Related entities are not updated after changing id in EventSubscriber (onFlush) -
i use composite primary keys doctrine entities (multi-tenancy).
example (yaml file):
id: website: associationkey: true id: type: integer
so have auto increment id manually. entities have id 1 default.
example (eventsubscriber):
class multitenanteventsubscriber implements eventsubscriber { public function getsubscribedevents() { return [ 'onflush' ]; } public function onflush(onflusheventargs $event) { $em = $event->getentitymanager(); $uow = $em->getunitofwork(); foreach ($uow->getscheduledentityinsertions() $entity) { if ($entity instanceof autoincrementinterface) { $maxid = $em ->createquerybuilder('e') ->select('max(e.id)') ->from(get_class($entity), 'e') ->getquery() ->getsinglescalarresult(); if ($maxid > 1) { $entity->setid($maxid + 1); $md = $em->getclassmetadata(get_class($entity)); $uow->recomputesingleentitychangeset($md, $entity); } } } } }
everything working far.
problem:
foreign key in related entities not updated when recompute change set. entity example auto incremented id 23 created correctly, entity b reference still returns reference id 1 instead of id 23.
$entitya = new a(); $entitya->setvalue('foo'); $entityb = new b(); $entityb->setvalue('bar'); $entityb->seta($entitya); $em->persist($entitya); $em->persist($entityb); $em->flush(); echo $entitya->getid(); // returns 23 echo $entityb->geta()->getid() // returns 1 instead of 23
of course update entityb in eventsubscriber, need generic solution because there lot of entities different relations.
maybe has idea how "recompute"/"refresh" related entities?
Comments
Post a Comment