RSS Feed
^__^

Distinct in Doctrine

Ivo Bathke, 2011/04/16 15:51

Wenn man DISTINCT in einem Query und Doctrine nutzen will muss man mit Aliasen arbeiten!
Sonst baut Doctrine einem da immer die id mit in den Query und das DISTINCT wird damit ausgehebelt.

So gehts nicht:

Doctrine::getTable('propose')
->createQuery('propose')
->select('propose.cat')
->distinct()
->fetchArray();

Denn das wird dazu:

SELECT DISTINCT p.id AS p__id, p.cat AS p__cat FROM propose p

so gehts:

Doctrine::getTable('propose')
->createQuery('propose')
->select('propose.cat as cat')
->distinct()
->fetchArray();

Denn das wird dazu:

SELECT DISTINCT p.cat AS p__0 FROM propose p

One Response to “Distinct in Doctrine”

  1. Ivo Bathke says:

    man alternativ kann auch group benutzen:

    Doctrine::getTable('propose')
    ->createQuery('propose')
    ->select('propose.cat as cat')
    ->groupBy('cat')
    ->orderBy('cat')
    ->fetchArray();
    

    ist wahrscheinlich besser mit doctrine

Leave a Reply