Use of DISTINCT in model find()

Hey,

according to the docs, Model::count() supports an array with a 'distinct' parameter. However, using Model::find() this parameter seems not to work. How can I pass a DISTINCT clause to a models find() method?

Thanks in advance. Best, Philipp

You can use phql

$phql = "SELECT DISTINCT (name) as name FROM ModelClass ORDER BY name";

$rows = $this->modelsManager->executeQuery($phql);
foreach($rows as $row)
{
        print_r($row)
}
edited Oct '14

Im sorry, but what about two or more columns? Only raw query? F.e. the next:

select distinct source_real, target_real from wide_exchange_rates where source_real != target_real

SELECT DISTINCT(m.id) as id, m.body, r.id as rId FROM modelName as m LEFT JOIN joinedModel as r ON r.messageId = m.id

WHERE m.recipientId = 3

Why DISTINCT don't work?



2.1k

Philipp is right, but DISTINCT used to work properly on Phalcon version < 1.3.0.

ok, so use it like that add GROUP BY clause

$phql = "SELECT DISTINCT (name) as name FROM ModelClass GROUP BY name ORDER BY name";

$rows = $this->modelsManager->executeQuery($phql);
foreach($rows as $row)
{
        print_r($row->toArray());
}


2.1k

thanks for this workaround Karol but DISTINCT is a common valid SQL clause that must work again.

As i said it used to work properly before 1.3 like other functions like CAST and CONVERT. I've already opened a discussion about these ones.

I have no doubt that Phalcon Team will fix the issue soon, but this kind of SQL feature can't stop to function after a version update.

Wait & See :)

You are right but for now you can use it, after Phalcon fix it GROUP BY will not break your application. Thx for adding comment for bug issue