views:

23

answers:

1

I am trying to perform a simple join on two tables and am having trouble finding the right syntax to use - I know i am missing something simple.

From the 'users' table i need the id (working fine) and from the 'meta' table i need the last_name (not working)

$q = Doctrine_Query::create()
->from('Users u')
->leftJoin('u.Meta m');

$users = $q->execute();

//attempt 1    
foreach($users as $user){
    $user_array[$user->id] = $user->last_name;
}

//attempt 2   
foreach($users as $user){
    $user_array[$user->id] = $user->m.last_name;
}

I've also tried adding an explicit select(u.id, m.last_name) to the query but the result is always the same

Uncaught exception 'Doctrine_Record_UnknownPropertyException' with message
'Unknown record property / related component "last_name" on "Users"

Obviously the property I am trying to access is not in the users table. -The query works fine, as if I don't request the last_name field - my application works as expected..

//proof   
foreach($users as $user){
    $user_array[$user->id] = 'fake_last_name!';
}

Can anyone provide a simple example of how I'm meant to do this (even a link to a page in the documentation) all of the examples of JOINING DQL in the doucmentation output the query to SQL instead of iterating the results...

Doctrine Documentation: Join Syntax

+1  A: 

I managed to work it out...

http://www.doctrine-project.org/documentation/manual/1_2/en/working-with-models

foreach($users as $user){
    $user_array[$user->id] = $user->Meta->last_name;
}
calumbrodie