diff --git a/Sum/DataTables/Engine/BaseEngine.php b/Sum/DataTables/Engine/BaseEngine.php index 5075761..62d8765 100644 --- a/Sum/DataTables/Engine/BaseEngine.php +++ b/Sum/DataTables/Engine/BaseEngine.php @@ -8,8 +8,8 @@ abstract class BaseEngine private static $_remote = FALSE; private static $_request = array(); protected $columns = array(); - protected $limit = - 1; - protected $offset = 0; + protected $limit; + protected $offset; protected $order = array(); protected $bound = array(); protected $search = array(); @@ -30,10 +30,10 @@ public static function setRequest($request) protected function request($index, $default = NULL) { - if (! is_array(self::$_request)) + if (empty(self::$_request)) self::$_request = $_REQUEST; - if (! isset(self::$_request[$index])) + if (empty(self::$_request[$index])) return $default; return self::$_request[$index]; @@ -96,7 +96,7 @@ protected function handlePaging() $this->output['draw'] = $this->request('draw', 0); if (isset(self::$_request['start']) AND $this->request('length') != - 1) { - $this->offset = $this->request('start'); + $this->offset = (string) $this->request('start'); $this->limit = $this->request('length'); return $this; diff --git a/Sum/DataTables/Engine/Pdo.php b/Sum/DataTables/Engine/Pdo.php index 233da65..71d7948 100644 --- a/Sum/DataTables/Engine/Pdo.php +++ b/Sum/DataTables/Engine/Pdo.php @@ -13,6 +13,7 @@ class Pdo extends BaseEngine private $_having; private $join = array(); private $_where; + private $_limited; private $_query; private $_requestQuery; @@ -142,8 +143,8 @@ public function build() if (! empty($this->search['column'])) $this->_requestQuery .= $glue . implode(' AND ', $this->search['column']); - empty($this->_groups) OR $this->_query .= " GROUP BY " . $this->_groups; - empty($this->_having) OR $this->_query .= " HAVING " . $this->_having; + empty($this->_groups) OR $this->_requestQuery .= " GROUP BY " . $this->_groups; + empty($this->_having) OR $this->_requestQuery .= " HAVING " . $this->_having; if ($this->handleOrdering()) { $this->_requestQuery .= " ORDER BY "; @@ -152,7 +153,10 @@ public function build() ($dir !== end($this->order)) AND $this->_requestQuery .= ', '; } } - $this->handlePaging() AND $this->_requestQuery .= " LIMIT {$this->offset},{$this->limit}"; + if($this->handlePaging()) { + $this->_limited = "LIMIT {$this->limit}"; + $this->offset AND $this->_limited .= " OFFSET {$this->offset}"; + } return $this; } @@ -164,17 +168,16 @@ public function make($asArray = FALSE) $this->_connection->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_NUM); $data = $this->_connection->prepare( - "SELECT SQL_CALC_FOUND_ROWS {$this->_select} FROM {$this->_table} {$this->_requestQuery}" + "SELECT {$this->_select} FROM {$this->_table} {$this->_requestQuery} {$this->_limited}" ); - $resFilterLength = $this->_connection->prepare("SELECT FOUND_ROWS()"); - - $resTotalLength = $this->_connection->prepare("SELECT COUNT(1) FROM {$this->_table} {$this->_query}"); + $resFilterLength = $this->_connection->prepare("SELECT count(*) FROM {$this->_table} {$this->_requestQuery}"); + $resTotalLength = $this->_connection->prepare("SELECT COUNT(*) FROM {$this->_table} {$this->_query}"); - $resFilterLength->execute(); - $this->output['recordsFiltered'] = $resFilterLength->fetch()[0]; - $resTotalLength->execute($this->bound); - $this->output['recordsTotal'] = $resTotalLength->fetch()[0]; + $resFilterLength->execute($this->bound); + $this->output['recordsFiltered'] = $resFilterLength->fetchColumn(); + $resTotalLength->execute(); + $this->output['recordsTotal'] = $resTotalLength->fetchColumn(); $data->execute($this->bound); $this->output['data'] = $data->fetchAll(); if ($asArray) {