0

I's like to do a join between 2 tables on a specific ID. At the moment, I have this DQL:

$q = Doctrine_Query::create()
         ->select('e.*, i.itemName, i.itemtypeId')
         ->from('Model_EventItem e')
         ->leftJoin('Model_Item i ON e.itemId = i.itemId')
         ->where('e.eventitemId = ?', $event->eventId)
         ->orderBy('i.itemName ASC');

The result is empty, although my eventId has a value ... Can you help me please? I there somewhere a tutorial on DQL-joins? I don't get it right with the help of the Doctrine documentation.

Thanks!

PS I have doctrine working in combination with Zend Framework.

koenHuybrechts
  • 868
  • 4
  • 15
  • 28
  • 1
    Is `$q` empty after you use the `execute()` function on the DQL? Because this query in your question won't do anything with the database. – DrColossos Jun 21 '10 at 16:37
  • Indeed, I have to execute .... BUt is the query correct? – koenHuybrechts Jun 21 '10 at 18:12
  • try `->leftJoin(e.Model_Item i)` the `ON` clause is added by Doctrine accodring to your mapping. For further examples check out http://www.doctrine-project.org/projects/orm/1.2/docs/manual/dql-doctrine-query-language/en#join-syntax – DrColossos Jun 22 '10 at 08:03

3 Answers3

1

you need add a relation to the model and join the tables using the relation

$q = Doctrine_Query::create()
     ->select('e.*, i.itemName, i.itemtypeId')
     ->from('Model_EventItem e')
     ->leftJoin('Model_EventItem.Model_Item i')
     ->where('e.eventitemId = ?', $event->eventId)
     ->orderBy('i.itemName ASC');
Andreas Linden
  • 12,489
  • 7
  • 51
  • 67
1

you should change the name in the left join from Model_EventItem to e

$q = Doctrine_Query::create()
     ->select('e.*, i.itemName, i.itemtypeId')
     ->from('Model_EventItem e')
     ->leftJoin('Model_EventItem.Model_Item i')
     ->where('e.eventitemId = ?', $event->eventId)
     ->orderBy('i.itemName ASC');
0
$q = Doctrine_Query::create()
     ->select('e.*, i.itemName, i.itemtypeId')
     ->from('Model_EventItem e, e.Model_Item i')
     ->where('e.eventitemId = ?', $event->eventId)
     ->orderBy('i.itemName ASC');
Tom
  • 30,090
  • 27
  • 90
  • 124
  • There is something wrong zith my relations in the schema ... This error pops up: Message: Unknown relation alias Model_Item – koenHuybrechts Jun 22 '10 at 12:42
  • Yep, best to check that. Doctrine models are usually written in camel case rather than with underscores. – Tom Jun 22 '10 at 14:12