/** * Provides additions to the ___load query for when selectors or selector string are provided * */ protected function getLoadQuerySelectors($selectors, DatabaseQuerySelect $query) { $database = $this->wire('database'); if (is_object($selectors) && $selectors instanceof Selectors) { // iterable selectors } else { if ($selectors && is_string($selectors)) { // selector string, convert to iterable selectors $selectors = new Selectors($selectors); } else { // nothing provided, load all assumed return $query; } } $functionFields = array('sort' => '', 'limit' => '', 'start' => ''); foreach ($selectors as $selector) { if (!$database->isOperator($selector->operator)) { throw new WireException("Operator '{$selector->operator}' may not be used in {$this->className}::load()"); } if (in_array($selector->field, $functionFields)) { $functionFields[$selector->field] = $selector->value; continue; } if (!in_array($selector->field, $fields)) { throw new WireException("Field '{$selector->field}' is not valid for {$this->className}::load()"); } $selectorField = $database->escapeTableCol($selector->field); $value = $database->escapeStr($selector->value); $query->where("{$selectorField}{$selector->operator}'{$value}'"); // QA } if ($functionFields['sort'] && in_array($functionFields['sort'], $fields)) { $query->orderby("{$functionFields['sort']}"); } if ($functionFields['limit']) { $query->limit(($functionFields['start'] ? (int) $functionFields['start'] . "," : '') . $functionFields['limit']); } return $query; }
protected function getQueryStartLimit(DatabaseQuerySelect $query, $selectors) { $start = null; $limit = null; $sql = ''; foreach ($selectors as $selector) { if ($selector->field == 'start') { $start = (int) $selector->value; } else { if ($selector->field == 'limit') { $limit = (int) $selector->value; } } } if ($limit) { $this->limit = $limit; if (is_null($start) && ($input = $this->fuel('input'))) { // if not specified in the selector, assume the 'start' property from the default page's pageNum $pageNum = $input->pageNum - 1; // make it zero based for calculation $start = $pageNum * $limit; } if (!is_null($start)) { $sql .= "{$start},"; $this->start = $start; } $sql .= "{$limit}"; if ($this->limit > 1) { $query->select("SQL_CALC_FOUND_ROWS"); } } if ($sql) { $query->limit($sql); } }
/** * Builds LIMIT clause for the query */ protected function _buildQueryLimit(DatabaseQuerySelect $query) { $limit = $this->getState()->limit; if ($limit) { $offset = $this->getState()->offset; $total = $this->getTotal(); //If the offset is higher than the total recalculate the offset if ($offset !== 0 && $total !== 0) { if ($offset >= $total) { $offset = floor(($total - 1) / $limit) * $limit; $this->getState()->offset = $offset; } } $query->limit($limit, $offset); } }