tags:

views:

247

answers:

2

I'm trying to execute a query but I get an error:

Unknown table alias

The tables are setup as follows:

Template_Spot hasOne  Template
Template      hasMany Template_Spot
Template      hasMany Location
Location      hasOne  Template

I'm trying to execute the following DQL:

$locationid = 1;
$spots = Doctrine_Query::create()
    ->select('cts.*, ct.*, uc.*')
    ->from('Template_Spot cts')
    ->innerJoin('Template ct')
    ->innerJoin('Location uc')
    ->where('uc.locationid = ?', $locationid)->execute();

Does anyone spot a problem?

A: 

In case you select all fields, ->select() is not needed at all.

Shouldn't it be:

$spots = Doctrine_Query::create()
    ->from('Template_Spot cts')
    ->leftJoin('Template ct')
    ->leftJoin('Location uc')
    ->where('uc.id = ?', $locationid)
    ->execute();
takeshin
+1  A: 

Try finding out which of the table aliases is not being recognised. I think it's trying to join Template_Spot with Location, in which case you may need to do:

[pseudo code]
FROM Template_Spot cts, cts.Template ct, ct.Location uc

Sometimes just defining Alias and foreignAlias names in your schema may also help, Doctrine gets confused easily. With multiple joins like this, Doctrine may also sometimes generate more SQL queries than necessary and you may wish to bypass DQL completely.

Tom
well it should be cts.Template ct and cts.Location uc because Template_Spot is a refclass with extra columns. Should these be loaded differently?
Ropstah