1

With doctrine syntax using postgres as database, is it possible to generate a query like:

SELECT 
DISTINCT ON (people.email)
*
FROM people
WHERE people.company SIMILAR TO '%companyA%|%companyB%|%...';

So far I have:

[...]
$query= Doctrine_Query::create()->from('People p')
        ->select('p.email, p.name, p.surname');

$alias= $query->getRootAlias();

foreach ($companies as $company){
    $query->orWhere($alias.'.name ilike ?', '%'.$company.'%');
}
[...]

I'm interested in the equivalent of "DISTINCT ON" for Doctrine ORM

Thanks

hmontoliu
  • 3,960
  • 1
  • 19
  • 21

1 Answers1

0

It seems that Docrine doesn't have a good way to use "DISTINCT ON". Also I haven't find a way using "DISTINCT" or ->distinct() that suits the needs I had (I got many crappy stuff when Doctrine builds the SQL query).

Finally I've done the DISTINCT ON stuff using a different approach. Here's the SQL query I'm using now:

SELECT * 
FROM people 
WHERE id IN (SELECT min(id) 
             FROM people
             WHERE company 
             SIMILAR TO '%companyA%|%companyB%|%...')
GROUP BY company;

The Doctrine syntax I've used to generate it from symfony is more or less:

$query = Doctrine_Query::create()->from('Company c')
         ->select('.....');
$innerquery  = Doctrine_Query::create()->from('Company ic')
         ->select('MIN(ic.id) AS id'); // important the "AS id"

$innerquery = (... stuff to get the company A, B, C in an andWhere() ...)

$innerquery->groupBy('ic.company');
$query->Where('c.id IN ('.$innerquery.')');
hmontoliu
  • 3,960
  • 1
  • 19
  • 21