Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-812

DQL hydration of joined objects skipped when main instance already present in entity manager

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-BETA4
    • Fix Version/s: 2.0-RC1
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      I do the following:

      $author1 = $em->find('Author', 1);
      
      $author1again = $em
        ->createQuery(
          'SELECT A, B '.
          'FROM Author A '.
          'INNER JOIN A.Books B '.
         'WHERE A.ID = 1'
        )
        ->getSingleResult();
      

      the Books collection is not hydrated because the author instance was already
      hydrated when populating $author1. This means when I call
      count($author1->getBooks()) a third query is executed to fetch the Books again.

      The result is even more problematic when adding a WITH clause to the
      INNER JOIN, in that case one would expect the Books collection to only
      contain the books matching matching that criteria.

      The result is expected when commenting out the $author1 =... line, thus when
      the author does not already exist in the entity manager.

      I'm sorry I can't produce a test case right now.

        Activity

        Hide
        Benjamin Eberlei added a comment -

        fix formatting

        Show
        Benjamin Eberlei added a comment - fix formatting
        Hide
        Roman S. Borschel added a comment -

        Fixed in master. Note that your second case still "won't work" from your point of view. If the collection is already initialized it is not overridden. This is consistent with other behavior (what is in-memory is preserved). Fetch-joinining a collection and using WITH at the same time is not a nice thing to do since the meaning of the association is changed per-query. First $author->getBooks() means one thing (all books written by this author) and a few lines later something else (all books written by this author in the last 10 years or whatever). To get the desired result in your second case, that is, overriding the collection in-memory, set the REFRESH query hint. Note that this will change the meaning of Author#books though, as is always the case with fetch-join + WITH.

        Show
        Roman S. Borschel added a comment - Fixed in master. Note that your second case still "won't work" from your point of view. If the collection is already initialized it is not overridden. This is consistent with other behavior (what is in-memory is preserved). Fetch-joinining a collection and using WITH at the same time is not a nice thing to do since the meaning of the association is changed per-query. First $author->getBooks() means one thing (all books written by this author) and a few lines later something else (all books written by this author in the last 10 years or whatever). To get the desired result in your second case, that is, overriding the collection in-memory, set the REFRESH query hint. Note that this will change the meaning of Author#books though, as is always the case with fetch-join + WITH.

          People

          • Assignee:
            Roman S. Borschel
            Reporter:
            Dennis Verspuij
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: