0

Here is my mappings:

$rsm = new ResultSetMapping;
$rsm->addEntityResult('OOHMediaBundle:OfferItem', 'i');
    $rsm->addFieldResult('i', 'reserved_at', 'reservedAt');

$rsm->addJoinedEntityResult('OOHMediaBundle:Offer', 'o', 'i', 'offer');
    $rsm->addFieldResult('o', 'o_id', 'id');
    $rsm->addFieldResult('o', 'continue_from', 'continueFrom');
    $rsm->addFieldResult('o', 'continue_to', 'continueTo');

Here is my native query:

$qb = $this->registry->getEntityManager()->createNativeQuery(
    'SELECT i.reserved_at, o.id AS o_id, o.continue_from, o.continue_to
    FROM offer_item AS i
    LEFT JOIN offers AS o ON i.offer_id = o.id
    WHERE i.reserved_at IS NOT NULL
    ;',
    $rsm
);

If above SQL is copied into mysql client it produce 43 records.

When executed as $qb->getArrayResult(); it only return 1 record.

When executed as $qb->getResult(); it return exception:

[Symfony\Component\Debug\Exception\ContextErrorException]  
Notice: Undefined index: offer_id 

Where other 42 records disappeared?

przemo_li
  • 3,932
  • 4
  • 35
  • 60
  • 1
    Did you try with `$qb->getResult();` ? – scoolnico Nov 24 '15 at 11:46
  • 1
    Try adding `i.id` to the field results. As far as I remember the `AI` columns were required as well Also keep in mind the order of the select clause matters and need to match the order in `addFieldResult()`. – Artamiel Nov 24 '15 at 11:54
  • I do not have id in that table :( its M2M joining table without ids... – przemo_li Nov 24 '15 at 12:01
  • @Artamiel pls turn Your comment into answer. It was in deed what I needed. (Thankfully 'id' was also needed for various other reasons there!) – przemo_li Nov 24 '15 at 12:44

1 Answers1

2

All right, as I mentioned in my comment, when using ResultSetMapping the Auto Increment column of the main entity needs to be listed in the field set result. The following should also apply for every join clause that needs to be added, so that doctrine can format the result properly.

The order of the columns added with addFieldResult() have to be same as the one listed in the RAW query.

The following chapter Examples in Doctrine's documentation contains some nice samples for further reference.

Artamiel
  • 3,652
  • 2
  • 19
  • 24