Uploaded image for project: 'Doctrine 2 - ORM'
  1. Doctrine 2 - ORM
  2. DDC-3084

Native query removes duplicate root entities

    Details

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

      Description

      I have a query like that:

              $rsm = new ResultSetMappingBuilder($this->_em);
              $rsm->addScalarResult('rank', 'rank');
              $rsm->addRootEntityFromClassMetadata('Application\Entity', 'e');
      
      		$query = $this->_em->createNativeQuery('
                  SELECT
                  	r.rank, ' . $rsm->generateSelectClause(array('e')) . '
                  FROM ...
      	    ', $rsm);
      
              return $query->getResult();
      

      When I issue it at the mysql side it returns 3 rows but doctrine returns only 2 - I have tried getArrayResults() with the same results

      The query could return the same entity multiple times for different values of the "rank" scalar value but it seems that doctrine removes those duplicates

        Activity

        Hide
        sparrowek Przemyslaw Wrobel added a comment -

        the result should be like this: ("rank", "entity")
        1, entity1
        2, entity2
        3, entity2

        Show
        sparrowek Przemyslaw Wrobel added a comment - the result should be like this: ("rank", "entity") 1, entity1 2, entity2 3, entity2
        Hide
        ocramius Marco Pivetta added a comment -

        Doctrine ORM groups the values of the root of the selection by identifier during hydration: this is the expected result.

        Show
        ocramius Marco Pivetta added a comment - Doctrine ORM groups the values of the root of the selection by identifier during hydration: this is the expected result.
        Hide
        sparrowek Przemyslaw Wrobel added a comment -

        If it is really grouping than I shoud have something like that
        entity1, 1
        entity2, array(2,3)

        Now the problem is that I am losing data - the row: 3, entity2 is missing and I have to resort to DBAL query or do not map to entities but than I cannot use my model logic

        Show
        sparrowek Przemyslaw Wrobel added a comment - If it is really grouping than I shoud have something like that entity1, 1 entity2, array(2,3) Now the problem is that I am losing data - the row: 3, entity2 is missing and I have to resort to DBAL query or do not map to entities but than I cannot use my model logic
        Hide
        ocramius Marco Pivetta added a comment -

        The resultset you proposed doesn't make sense to me, as it contains inhomogeneous data.

        I'd just suggest skipping over adding a root entity to the RSM.

        Show
        ocramius Marco Pivetta added a comment - The resultset you proposed doesn't make sense to me, as it contains inhomogeneous data. I'd just suggest skipping over adding a root entity to the RSM.
        Hide
        fran6co Francisco Facioni added a comment -

        How do you hydrate the entities without setting a root entity to the RSM?

        Show
        fran6co Francisco Facioni added a comment - How do you hydrate the entities without setting a root entity to the RSM?
        Hide
        fran6co Francisco Facioni added a comment -

        It looks like there is some commented code that would fix this issue => https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php#L569

        Show
        fran6co Francisco Facioni added a comment - It looks like there is some commented code that would fix this issue => https://github.com/doctrine/doctrine2/blob/2.4/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php#L569

          People

          • Assignee:
            ocramius Marco Pivetta
            Reporter:
            sparrowek Przemyslaw Wrobel
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: