Search code examples
symfonydoctrine-ormdql

Join two entities in symfony2 using doctrine2


There are two entities. Ticket and Device.

In device orm there is

<one-to-many target-entity="Ticket" mapped-by="Device" field="ticket"/>
<many-to-one field="category" target-entity="Category"/>

in ticket orm

<many-to-one field="device"  target-entity="Device"/>

I want to implement a filter where the user can filter tickets by device's category. How can I do that? I tried with

$qb->select(array('t', 'd'))
        ->from('MyBundle:Ticket', 't')
        ->innerJoin('t.device', 'd')
        ->where("t.category.name = 'Cashbox'");;

But this gives me an error

[Syntax Error] line 0, col 88: Error: Expected =, <, <=, <>, >, >=, !=, got '.'

Solution

  • ->where("t.category.name = 'Cashbox'");;
    

    is incorrect. You have to join the category table:

    $qb
        ->select(array('t', 'd'))
        ->from('MyBundle:Ticket', 't')
        ->innerJoin('t.device', 'd')
        ->innerJoin('d.category', 'c')
        ->where("c.name = 'Cashbox'");