From 2db53058c2131e13a9809f4aa6c12a4b13bef571 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 10 Jan 2013 00:27:46 +0100 Subject: [PATCH] Implementing a draft version of DISTINCT ON with a fallback --- lib/Cake/Model/Datasource/Database/Query.php | 6 +++--- .../Test/TestCase/Model/Datasource/Database/QueryTest.php | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Cake/Model/Datasource/Database/Query.php b/lib/Cake/Model/Datasource/Database/Query.php index 6e3e4522691..816854f06ce 100644 --- a/lib/Cake/Model/Datasource/Database/Query.php +++ b/lib/Cake/Model/Datasource/Database/Query.php @@ -202,9 +202,9 @@ protected function _buildSelectPart($parts) { } if (is_array($this->_distinct)) { - //Supports DISTINCT ON? - if (false) { - + //todo: ask driver if it cannot support distinct on + if (true) { + $this->group($this->_distinct, true); } else { $distinct = sprintf('DISTINCT ON (%s) ', implode(', ', $this->_distinct)); } diff --git a/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php b/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php index c71c63dddc4..625b7b15957 100644 --- a/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php +++ b/lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php @@ -1075,6 +1075,12 @@ public function testSelectDistinct() { $result = $query->distinct()->execute(); $this->assertCount(2, $result); + + $result = $query->select(['id'])->distinct(false)->execute(); + $this->assertCount(3, $result); + + $result = $query->select(['id'])->distinct(['author_id'])->execute(); + $this->assertCount(2, $result); } }