2

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!

6
  • Try making the join like this: ->innerJoin('r.auto', 'auto) and ->innerJoin('r.abteilung', 'abteilung) Commented Dec 18, 2014 at 18:16
  • Then there is no syntax error but i get NO result for the table 'abteilung' Commented Dec 18, 2014 at 18:21
  • this is because you are only selecting 'r' at ->createQueryBuilder('r') Commented Dec 18, 2014 at 18:23
  • i testet it without joins, the joins are useless they don't give more data then without. whats that? Commented Dec 18, 2014 at 18:26
  • Of course, in your case, joins do nothing cause you are not retrieving data across them. In your query you only are selection Rents where date is between. Nothing more. Commented Dec 18, 2014 at 18:30

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.