From 1a69c2abef87d738c53a89cd6a2ee54640a0c007 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 5 Dec 2014 05:42:32 +0000
Subject: [PATCH] orm: bug fixes

---
 include/class.orm.php | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index c3b815318..b8977f0a0 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -1028,8 +1028,7 @@ class ModelInstanceManager extends ResultSet {
         // be the root model's fields. The annotated fields will be wrapped
         // using an AnnotatedModel instance.
         if ($annotations && $modelClass == $this->model) {
-            foreach ($annotations as $A) {
-                $name = $A->getAlias();
+            foreach ($annotations as $name=>$A) {
                 if (isset($fields[$name])) {
                     $extras[$name] = $fields[$name];
                     unset($fields[$name]);
@@ -1798,6 +1797,9 @@ class MySqlCompiler extends SqlCompiler {
                 }
                 // TODO: Throw exception if $field can be indentified as
                 //       invalid
+                if ($field instanceof SqlFunction)
+                    $field = $field->toSql($this, $model);
+
                 $orders[] = $field.' '.$dir;
             }
             $sort = ' ORDER BY '.implode(', ', $orders);
@@ -1883,8 +1885,8 @@ class MySqlCompiler extends SqlCompiler {
         $group_by = array();
         // Add in annotations
         if ($queryset->annotations) {
-            foreach ($queryset->annotations as $A) {
-                $fields[] = $T = $A->toSql($this, $model, true);
+            foreach ($queryset->annotations as $alias=>$A) {
+                $fields[] = $T = $A->toSql($this, $model, $alias);
                 // TODO: Add to last fieldset in fieldMap
                 if ($fieldMap)
                     $fieldMap[0][0][] = $A->getAlias();
-- 
GitLab