Merge pull request #11635 from civicrm/4.7.30-rc
[civicrm-core.git] / Civi / API / SelectQuery.php
index 06ad6c73f43570bfefed8c1a5abd8cb0cffe904f..b052ec41898880540004e8ce871ad9c08b20e118 100644 (file)
@@ -402,21 +402,24 @@ abstract class SelectQuery {
    * @throws \Civi\API\Exception\UnauthorizedException
    */
   protected function buildOrderBy() {
-    $orderBy = array();
     $sortParams = is_string($this->orderBy) ? explode(',', $this->orderBy) : (array) $this->orderBy;
-    foreach ($sortParams as $item) {
-      $words = preg_split("/[\s]+/", trim($item));
+    foreach ($sortParams as $index => $item) {
+      $item = trim($item);
+      if ($item == '(1)') {
+        continue;
+      }
+      $words = preg_split("/[\s]+/", $item);
       if ($words) {
         // Direction defaults to ASC unless DESC is specified
         $direction = strtoupper(\CRM_Utils_Array::value(1, $words, '')) == 'DESC' ? ' DESC' : '';
         $field = $this->getField($words[0]);
         if ($field) {
-          $orderBy[] = self::MAIN_TABLE_ALIAS . '.' . $field['name'] . $direction;
+          $this->query->orderBy(self::MAIN_TABLE_ALIAS . '.' . $field['name'] . $direction, NULL, $index);
         }
         elseif (strpos($words[0], '.')) {
           $join = $this->addFkField($words[0], 'LEFT');
           if ($join) {
-            $orderBy[] = "`{$join[0]}`.`{$join[1]}`$direction";
+            $this->query->orderBy("`{$join[0]}`.`{$join[1]}`$direction", NULL, $index);
           }
         }
         else {
@@ -424,7 +427,6 @@ abstract class SelectQuery {
         }
       }
     }
-    $this->query->orderBy($orderBy);
   }
 
   /**
@@ -465,8 +467,10 @@ abstract class SelectQuery {
       }
     }
 
-    // Always select the ID.
-    $this->selectFields[self::MAIN_TABLE_ALIAS . ".id"] = "id";
+    // Always select the ID if the table has one.
+    if (array_key_exists('id', $this->apiFieldSpec)) {
+      $this->selectFields[self::MAIN_TABLE_ALIAS . ".id"] = "id";
+    }
 
     // core return fields
     foreach ($return as $fieldName) {