mixed resultset mapping for multiple entity #6147

Closed
opened 2026-01-22 15:27:40 +01:00 by admin · 1 comment
Owner

Originally created by @tugrul on GitHub (Dec 22, 2018).

Originally assigned to: @Ocramius on GitHub.

Q A
Version 2.9.0

Support Question

I want to get mixed entity mapping of native query resultset like following.

public function getUpdatedListBySubforum(Subforum $subforum)
    {
        $em = $this->getEntityManager();

        $rsm = new ResultSetMappingBuilder($em);

        $rsm->addRootEntityFromClassMetadata(Thread::class, 'thread');
        $rsm->addRootEntityFromClassMetadata(Post::class, 'post', 
             ['id' => 'post_id', 'create_date' => 'post_create_date']);

        $query = 'select ' . $rsm->generateSelectClause(['thread' => 'WFT', 'post' => 'WFP']) . 
            ' from forum_thread WFT ' . 
            'inner join (select thread_id, max(id) as last_post_id from forum_post group by thread_id) LPI on WFT.id = LPI.thread_id '. 
            'inner join forum_post WFP on WFP.id = LPI.last_post_id where WFT.subforum_id = :subforumId';

        return $em->createNativeQuery($query, $rsm)
            ->setParameter(':subforumId', $subforum->getId())
            ->getResult();
    }

Actual Resultset

[
  {id: 1, class: Thread},
  {id: 1, class: Post},
  {id: 2, class: Thread},
  {id: 2, class: Post}
]

Expected Resultset

[
  ['thread': {id: 1, class: Thread}, 'post': {id: 1, class: Post}],
  ['thread': {id: 2, class: Thread}, 'post': {id: 2, class: Post}]
]
Originally created by @tugrul on GitHub (Dec 22, 2018). Originally assigned to: @Ocramius on GitHub. | Q | A |------------ | ----- | Version | 2.9.0 ### Support Question I want to get mixed entity mapping of native query resultset like following. ```php public function getUpdatedListBySubforum(Subforum $subforum) { $em = $this->getEntityManager(); $rsm = new ResultSetMappingBuilder($em); $rsm->addRootEntityFromClassMetadata(Thread::class, 'thread'); $rsm->addRootEntityFromClassMetadata(Post::class, 'post', ['id' => 'post_id', 'create_date' => 'post_create_date']); $query = 'select ' . $rsm->generateSelectClause(['thread' => 'WFT', 'post' => 'WFP']) . ' from forum_thread WFT ' . 'inner join (select thread_id, max(id) as last_post_id from forum_post group by thread_id) LPI on WFT.id = LPI.thread_id '. 'inner join forum_post WFP on WFP.id = LPI.last_post_id where WFT.subforum_id = :subforumId'; return $em->createNativeQuery($query, $rsm) ->setParameter(':subforumId', $subforum->getId()) ->getResult(); } ``` #### Actual Resultset ``` [ {id: 1, class: Thread}, {id: 1, class: Post}, {id: 2, class: Thread}, {id: 2, class: Post} ] ``` #### Expected Resultset ``` [ ['thread': {id: 1, class: Thread}, 'post': {id: 1, class: Post}], ['thread': {id: 2, class: Thread}, 'post': {id: 2, class: Post}] ]
admin added the DuplicateQuestion labels 2026-01-22 15:27:40 +01:00
admin closed this issue 2026-01-22 15:27:41 +01:00
Author
Owner

@Ocramius commented on GitHub (Dec 27, 2018):

I suggest comparing a ResultSetMapping to the ResultSetMapping of an arbitrary join.

See https://github.com/doctrine/doctrine2/issues/7221
See https://github.com/doctrine/doctrine2/pull/7255

Closing as duplicate: while this isn't ideal, this is how DQL handles multiple root entities.

@Ocramius commented on GitHub (Dec 27, 2018): I suggest comparing a `ResultSetMapping` to the `ResultSetMapping` of an arbitrary join. See https://github.com/doctrine/doctrine2/issues/7221 See https://github.com/doctrine/doctrine2/pull/7255 Closing as `duplicate`: while this isn't ideal, this is how DQL handles multiple root entities.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6147