Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2496

Unexpected behaviour flushing single entity

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.3.4
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:

      Description

      The following works as expected:
      $existingElement = $repository->find(1);
      $existingElement->setSomething('foo');
      $newElement = new Element('bar');
      $em->persist($newElement);
      $em->flush($newElement);
      // The new element is added and the existing is not modified

      The following does not:
      $existingElement = $repository->find(1);
      $em->remove(existingElement);
      $newElement = new Element('bar');
      $em->persist($newElement);
      $em->flush($newElement);
      // The new element is added and the existing is deleted (i didnt flush THAT element)

      The following does not:
      $newElement2 = new Element('foo');
      $em->persist($newElement2);
      $newElement = new Element('bar');
      $em->persist($newElement);
      $em->flush($newElement);
      // Both elements are persisted, even if I only flushed $newElement

        Activity

        Hide
        Hernan Rajchert added a comment -

        By the way, I noticed in the code that this seems to be "expected behaviour", but it doesn't make too much sense, as the idea is to not disturb the original unit of work.

        The problem arises when I call some findOrCreate methods that I've done. The idea is that those entities should be there, but not always are, and they usually occur on the middle of some other processing, so if I have some new elements before calling the findOrCreate I dont want them to be inserted until I flush them in particular.

        Right now Im creating a new EntityManager (which involves a new connection), doing the insert there and then merging into the other EntityManager, but the merge is very error prone to the order you merge stuff.

        Show
        Hernan Rajchert added a comment - By the way, I noticed in the code that this seems to be "expected behaviour", but it doesn't make too much sense, as the idea is to not disturb the original unit of work. The problem arises when I call some findOrCreate methods that I've done. The idea is that those entities should be there, but not always are, and they usually occur on the middle of some other processing, so if I have some new elements before calling the findOrCreate I dont want them to be inserted until I flush them in particular. Right now Im creating a new EntityManager (which involves a new connection), doing the insert there and then merging into the other EntityManager, but the merge is very error prone to the order you merge stuff.
        Hide
        Marco Pivetta added a comment -

        Flushing with a given parameter is only about optimizing changeset computation - it is not meant to flush only the new element.

        Show
        Marco Pivetta added a comment - Flushing with a given parameter is only about optimizing changeset computation - it is not meant to flush only the new element.
        Hide
        Hernan Rajchert added a comment -

        So how would you recommend to resolve the findOrCreate issues?

        Show
        Hernan Rajchert added a comment - So how would you recommend to resolve the findOrCreate issues?
        Hide
        Marco Pivetta added a comment -

        You would have to flush afterwards.

        Show
        Marco Pivetta added a comment - You would have to flush afterwards.

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Hernan Rajchert
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: