Ropstah
Ropstah

Reputation: 17804

Doctrine: Unknown table alias. Is this DQL correct?

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?

Upvotes: 0

Views: 1078

Answers (2)

Tom
Tom

Reputation: 30698

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.

Upvotes: 1

takeshin
takeshin

Reputation: 50648

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();

Upvotes: 0

Related Questions