3

I have some problems with Doctrine. Im trying to do a leftJoin, following this question: How to do left join in Doctrine?

Here is my query:

$qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('a', 'u')
        ->from('Application\Entity\Event', 'a')
        ->leftJoin(
            'Application\Entity\Venue',
            'u',
            \Doctrine\ORM\Query\Expr\Join::WITH,
            'a.venue_id = u.id'
        )
        ->orderBy('a.event_datetime', 'DESC');

    var_dump($qb->getQuery()->getResult());

And here are the results:

array (size=4)
0 => 
object(Application\Entity\Event)[483]
  protected 'id' => int 8
  protected 'artist_id' => int 4
  protected 'venue_id' => int 1246
  protected 'name' => string 'Vlasta Redl' (length=11)
1 => 
object(Application\Entity\Venue)[477]
  protected 'id' => int 1246
  protected 'name' => string 'Malostranská beseda' (length=20)
  ...
2 => 
object(Application\Entity\Event)[468]
  protected 'id' => int 7
  protected 'artist_id' => int 3
  protected 'venue_id' => int 761
  protected 'name' => string 'Positive Mind' (length=13)

3 => 
object(Application\Entity\Venue)[485]
  protected 'id' => int 761
  protected 'name' => string 'Divadlo pod lampou' (length=18)
  ....

This is an array of different objects. Events and Venues.

But I'm expecting such result:

array(
    array(
        0 => Event
        1 => Venue,
    ),
    array(
        0 => Event
        1 => Venue,
    ),
    // ...
)

What am I doing wrong?

Community
  • 1
  • 1
Jaroušek Puchlivec
  • 221
  • 1
  • 2
  • 11
  • You're not doing anything wrong, that's the way the result is displayed. – acontell Jan 15 '15 at 15:13
  • Here another similar question: http://stackoverflow.com/questions/25665014/sql-doctrine-getresult-in-leftjoin-returns-object-and-referenced-object-as-2-en But in the result you will lost all of your objects. – Jaroušek Puchlivec Jan 15 '15 at 16:41
  • That's not your case because you're retrieving objects and not scalars. You could try to build a custom hydrator – acontell Jan 15 '15 at 18:12
  • Well i thought its working out of the box. At least i remember that in some earlier version it was. – Jaroušek Puchlivec Jan 15 '15 at 19:50
  • the answer is here http://stackoverflow.com/questions/25665014/sql-doctrine-getresult-in-leftjoin-returns-object-and-referenced-object-as-2-en – heavyrick Jul 08 '16 at 14:10
  • the answer is here http://stackoverflow.com/questions/25665014/sql-doctrine-getresult-in-leftjoin-returns-object-and-referenced-object-as-2-en – heavyrick Jul 08 '16 at 14:14
  • Possible duplicate of [SQL/Doctrine: getResult in leftJoin returns object and referenced object as 2 entries in array](http://stackoverflow.com/questions/25665014/sql-doctrine-getresult-in-leftjoin-returns-object-and-referenced-object-as-2-en) – Veve May 12 '17 at 07:30

1 Answers1

0

I tried but I could not.

I have done so:

foreach($result as $object){
            if(get_class($object)=='Panel\PollBundle\Entity\PollProfile'){
                $pollProfile[] = $object;
            }
            else if (get_class($object)=='Panel\PollBundle\Entity\Poll') {
                $poll[] = $object;
            }
        }
websky
  • 3,047
  • 1
  • 33
  • 31