symfony and doctrine: select distinct column

by prettyscripts on 2010-06-16 16:28

phpsymfonysql

to select distinct column values from a table, there are a few things to note to make sure only distinct rows are returned.

in /path/to/lib/model/doctrine/SomeTable.class.php:

PHP

public function getDistinctValue() {
    $q Doctrine_Query()::create()
        ->select('DISTINCT column_name as some_column')
        ->from('SomeTable');
    return $q->fetchArray();
}

notes:

  • line 3: DISTINCT must be capitalized
  • line 3: 'as' must be used, without this everything will be returned
  • line 5: use fetchArray() instead of execute(), the latter doesn't work!