From f5b75a84638b03ef27c56cff1f24d47315d88a68 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 5 Dec 2014 05:43:48 +0000
Subject: [PATCH] orm: Add support for DISTINCT to SqlAggregate

---
 include/class.orm.php | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index b8977f0a0..a1328a6fd 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -612,7 +612,10 @@ class SqlAggregate extends SqlFunction {
     }
 
     function toSql($compiler, $model=false, $alias=false) {
-        $options = array('constraint'=>$this->constraint);
+        $options = array(
+                'constraint' => $this->constraint,
+                'distinct' => $this->distinct);
+
         list($field) = $compiler->getField($this->expr, $model, $options);
         return sprintf('%s(%s)%s', $this->func, $field,
             $alias && $this->alias ? ' AS '.$compiler->quote($this->alias) : '');
@@ -1373,6 +1376,10 @@ class SqlCompiler {
             $field = $alias.'.'.$this->quote($field);
         else
             $field = $this->quote($field);
+
+        if (isset($options['distinct']) && $options['distinct'])
+            $field = " DISTINCT $field";
+
         if (isset($options['model']) && $options['model'])
             $operator = $model;
         return array($field, $operator);
-- 
GitLab