Mikado Joe
Mikado Joe

Reputation: 183

Twice Join in Symfony2 with Doctrine (Syntax Error)

I have the tables:

Car(Auto) ->(1:N) Rent (N:1) <- Department(Abteilung)

I want to join them with an inner join together. In Rent are the IDs of department and car.

If I do a join twice I get an error. With one join it's working. Why is that? How can i fix this?

$result = $this->getDoctrine()->getRepository('ChrisKfzBuchungBundle:Rent')
            ->createQueryBuilder('r')
            ->innerJoin('ChrisKfzBuchungBundle:Rent','ChrisKfzBuchungBundle:Auto')
            ->innerJoin('ChrisKfzBuchungBundle:Rent','ChrisKfzBuchungBundle:Abteilung')
            ->where('r.mieteStart >= :date_from')
            ->andWhere('r.mieteEnde <= :date_to')
            ->setParameter('date_from', $date_from)
            ->setParameter('date_to', $date_to)
            ->distinct()
            ->getQuery()->getArrayResult();

[Syntax Error] line 0, col 129: Error: Expected Literal, got 'JOIN'

Thanks!

Upvotes: 2

Views: 259

Answers (1)

Mikado Joe
Mikado Joe

Reputation: 183

That works, thanks to manix for help. I had to correct the Joins and use addSelect (an me unknown command).

 $result = $this->getDoctrine()->getRepository('ChrisKfzBuchungBundle:Rent')
            ->createQueryBuilder('r')
            ->addSelect('abteilung')
            ->addSelect('auto')
            ->join('r.auto','auto')
            ->join('r.abteilung','abteilung')
            ->where('r.mieteStart >= :date_from')
            ->andWhere('r.mieteEnde <= :date_to')
            ->setParameter('date_from', $date_from)
            ->setParameter('date_to', $date_to)
            ->distinct()
            ->getQuery()->getArrayResult();

Upvotes: 1

Related Questions