From 508f3f81e2d0be55029f8fe28b3aa6f846f933be Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Tue, 22 Jan 2019 17:14:04 +0100 Subject: [PATCH] fix SQL error when ordering by custom-field as $order_by parameter can contain more then just order by columns, eg. group by clause --- api/src/Storage.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/src/Storage.php b/api/src/Storage.php index a160d02265..93d5cac8c0 100644 --- a/api/src/Storage.php +++ b/api/src/Storage.php @@ -536,6 +536,12 @@ class Storage extends Storage\Base // check if we order by a custom field --> join cf table for given cf and order by it's value if (strpos($order_by,self::CF_PREFIX) !== false) { + // if $order_by contains more then order by columns (eg. group by) split it off before + if (($pos = stripos($order_by, 'order by')) !== false) + { + $group_by = substr($order_by, 0, $pos+9); + $order_by = substr($order_by, $pos+9); + } // fields to order by, as cutomfields may have names with spaces, we examine each order by criteria $fields2order = explode(',',$order_by); foreach($fields2order as $v) @@ -578,6 +584,11 @@ class Storage extends Storage\Base $join .= $this->extra_join_order.' AND extra_order.'.$this->extra_key.'='.$this->db->quote($key); } } + // add group by again + if (isset($group_by)) + { + $order_by = $group_by.$order_by; + } } // check if we filter by a custom field if (is_array($filter))