Symfony2 and Doctrine: how to fetch two different object for the same id?

后端 未结 2 1540
花落未央
花落未央 2021-01-18 11:33

I have this scenario:

  • Object A have some reference to other objects B,C,D
  • Object B have some reference to other objects A,F,G
  • Object C have so
相关标签:
2条回答
  • 2021-01-18 12:12

    So far as I know this is not a supported D2 behaviour out of the box, so I would say you'll need to implement the clone operation yourself.

    Having said that, I'm also not 100% sure what'll happen if you serialize and then unserialize a D2 entity, this might do what you want (albeit in a rather unpleasant fashion). In addition you could, after fetching the entity initially, detach what you've received immediately and re-fetch the result again before any references to the 'live' copy have propagated outside of the original instantiation point (if the query's using the result cache this shouldn't carry too much of a penalty). Presumably this wouldn't be a tremendous problem however, as you're just working with the copy temporarily.

    WRT to clearing the entity manager, it'll detach all managed entities, which may well have undesirable side-effects.

    HTH.

    0 讨论(0)
  • 2021-01-18 12:17

    Answer was less complex than I expected.

    It seem to be sufficent call $this->entity_manager->clear(); that will clear this entity map and force it to reload from database into a brand new object!

    $this->entity_manager->clear();
    $aCopy = $this->entity_manager
                           ->getRepository('MyBundle:A')
                           ->find($a->getId());
                $this->logger->debug('Original Obj: '.spl_object_hash($a));
                $this->logger->debug('Copied Obj:      '.spl_object_hash($aCopy));
    

    this will print

    [2013-02-08 12:07:20] app.DEBUG: Original Obj: 000000006523645c000000004b1160d1 [] [] [2013-02-08 12:07:20] app.DEBUG: Copied Obj: 00000000652366e3000000004b1160d1 [] []

    0 讨论(0)
提交回复
热议问题