|
|
@@ -584,34 +584,61 @@ if(V::get('DBG_DS', 0, $_GET) > 0){echo'<pre style="max-height:200px;overflow:au
|
|
|
public function getItems($params = array()) {
|
|
|
$primaryKeyField = $this->getPrimaryKeyField();
|
|
|
$items = array();
|
|
|
- $sql_limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
|
|
|
- $sql_offset = V::get('limitstart', 0, $params, 'int');
|
|
|
- $sql_order_by = V::get('order_by', '', $params);
|
|
|
- if ($sql_order_by) {
|
|
|
- $sql_order_dir = V::get('order_dir', '', $params);
|
|
|
-
|
|
|
- // prevent from sorting by special columns
|
|
|
- if (!array_key_exists($sql_order_by, $this->_cols)) {
|
|
|
- $sql_order_by = null;
|
|
|
- $sql_order_dir = null;
|
|
|
+ $sql = new stdClass();
|
|
|
+ $sql->limit = V::get('limit', $this->_default_sql_limit, $params, 'int');
|
|
|
+ $sql->offset = V::get('limitstart', 0, $params, 'int');
|
|
|
+ $sql->orderBy = '';
|
|
|
+ $sql->_orderBy = V::get('order_by', '', $params);
|
|
|
+ $sql->_sortBy = V::get('sortBy', '', $params);
|
|
|
+ if ($sql->_sortBy) {// ID A,COL_X D,COL_Y A,...
|
|
|
+ $sql->_sortByList = array();
|
|
|
+ $sortByEx = explode(',', $sql->_sortBy);
|
|
|
+ foreach ($sortByEx as $sortPart) {
|
|
|
+ $sortPart = trim($sortPart);
|
|
|
+ if (empty($sortPart)) continue;
|
|
|
+ $sortPartEx = explode(' ', $sortPart);
|
|
|
+ if (count($sortPartEx) > 2) throw new Exception("SortBy parse error #" . __LINE__);
|
|
|
+ $sortColName = trim($sortPartEx[0]);
|
|
|
+ if (!array_key_exists($sortColName, $this->_cols)) throw new Exception("SortBy parse error - no column name '{$sortColName}' #" . __LINE__);
|
|
|
+ $colSortDir = 'ASC';
|
|
|
+ if (count($sortPartEx) == 2) {
|
|
|
+ if ('A' == $sortPartEx[1] || 'ASC' == $sortPartEx[1]) {
|
|
|
+ } else if ('D' == $sortPartEx[1] || 'DESC' == $sortPartEx[1]) {
|
|
|
+ $colSortDir = 'DESC';
|
|
|
+ } else throw new Exception("SortBy parse error - unknown sort order '{$sortPartEx[1]}' #" . __LINE__);
|
|
|
+ }
|
|
|
+ $sql->_sortByList[] = "t.`{$sortColName}` {$colSortDir}";
|
|
|
}
|
|
|
- }
|
|
|
- if ($sql_order_by) {
|
|
|
- $sql_order_by = "order by t.`{$sql_order_by}`";
|
|
|
- if ($sql_order_dir) {
|
|
|
- $sql_order_by = "{$sql_order_by} {$sql_order_dir}";
|
|
|
+ if (!empty($sql->_sortByList)) {
|
|
|
+ $sql->orderBy = "order by " . implode(", ", $sql->_sortByList);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if ($sql->_orderBy) {
|
|
|
+ $sql->_orderDir = V::get('order_dir', '', $params);
|
|
|
+
|
|
|
+ // prevent from sorting by special columns
|
|
|
+ if (!array_key_exists($sql->_orderBy, $this->_cols)) {
|
|
|
+ $sql->_orderBy = null;
|
|
|
+ $sql->_orderDir = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($sql->_orderBy) {
|
|
|
+ $sql->orderBy = "order by t.`{$sql->_orderBy}`";
|
|
|
+ if ($sql->_orderDir) {
|
|
|
+ $sql->orderBy = "{$sql->orderBy} {$sql->_orderDir}";
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- $sql_cols = $this->_get_sql_cols();
|
|
|
- $sql_where = $this->_parseSqlWhere($params);
|
|
|
- $sql = "select {$sql_cols}
|
|
|
- from {$this->_tbl} as t
|
|
|
- where {$sql_where}
|
|
|
- {$sql_order_by}
|
|
|
- limit {$sql_limit} offset {$sql_offset}
|
|
|
+ $sql->cols = $this->_get_sql_cols();
|
|
|
+ $sql->where = $this->_parseSqlWhere($params);
|
|
|
+ $sql->query = "select {$sql->cols}
|
|
|
+ from `{$this->_tbl}` t
|
|
|
+ where {$sql->where}
|
|
|
+ {$sql->orderBy}
|
|
|
+ limit {$sql->limit} offset {$sql->offset}
|
|
|
";
|
|
|
- if(V::get('DBG_DS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>'."\n\n";}
|
|
|
- $res = $this->_db->query($sql);
|
|
|
+ DBG::_('DBG_DS', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ $res = $this->_db->query($sql->query);
|
|
|
while ($r = $this->_db->fetch($res)) {
|
|
|
$items[$r->{$primaryKeyField}] = $r;
|
|
|
}
|