1

I want to use an inner Join for my tables auto and rent('s'). Its an 1:N relationship. If I use ->innerJoin('s', 'a') it comes to some errors like this:

[Semantical Error] line 0, col 62 near 's a, ChrisKfzBuchungBundle:Auto': Error: Class 's' is not defined.

$repo = $this->getDoctrine()->getRepository('ChrisKfzBuchungBundle:Rent');

$qb = $repo->createQueryBuilder('s');
$qb->from('ChrisKfzBuchungBundle:Auto', 'a')
    ->where('s.mieteStart >= :date_from')
    ->andWhere('s.mieteEnde <= :date_to')
    ->setParameter('date_from', $date_from)
    ->setParameter('date_to', $date_to);

How do I join one ore more tables with queryBuilder?

vimuth
  • 5,064
  • 33
  • 79
  • 116
Mikado Joe
  • 183
  • 1
  • 1
  • 14

1 Answers1

2

There are methods for that and also for me it just worked with a configured relation between the two entites. Here are two working examples:

left join:

$query = $repo->createQueryBuilder('s')
    ->leftJoin(ChrisKfzBuchungBundle:Auto', 'a', 'WITH', 's.id = a.yourJoinCOlumn')
    ...

inner join:

$query = $repo->createQueryBuilder('s')
    ->select('s, a')
    ->innerJoin('s.yourJoinColumn', 'a')
    ...
Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
  • [Semantical Error] line 0, col 90 near 's a WHERE s.mieteStart': Error: Class 's' is not defined. please post the complete code. where in your left join is a definded? – Mikado Joe Dec 15 '14 at 20:56