/** * get an array of data to use in the response * @param Query $query the query to use to retrieve the data * @param Paginator $paginator * @param array $additionalData an associative array of data to merge with the data array */ protected function getData($query, $additionalData = array()) { //use this hook to alter the parameters $paginator = null; if (Input::get('page') || $this->paginate) { $beforePagination = Event::fire('before.pagination', array(&$query)); //check if $object is a model or a relation $model = $query->getModel(); $model = method_exists($model, 'getRelated') ? $model->getRelated() : $model; $perPage = Input::get('pp') ?: $model->getPerPage(); $paginator = $query->paginate($perPage); //preserve the url query in the paginator $paginator->appends(Input::except('page')); } $results = isset($paginator) ? $paginator->getCollection() : $query->get(); $data = array(); $data[$this->resultsKey] = $this->isAjaxRequest() ? $results->toArray() : $results; $data['total'] = isset($paginator) ? $paginator->getTotal() : $data->{$this->resultsKey}->count(); if ($paginator) { $data['paginator'] = $paginator; } if (is_array($additionalData)) { $data = array_merge($data, $additionalData); } return $data; }
/** * Updates the total count of models. For better performance * the count is only updated on edges, which is when new models * need to be loaded. */ private function updateCount() { // The count only needs to be updated when the pointer is // on the edges if ($this->pointer % $this->limit != 0 && $this->pointer < $this->count) { return; } $model = $this->query->getModel(); $newCount = $model::totalRecords($this->query->getWhere()); // It's possible when iterating over models that something // is modified or deleted that causes the model count // to decrease. If the count has decreased then we // shift the pointer to prevent overflow. // This calculation is based on the assumption that // the first N (count - count') models are deleted. if ($this->count != 0 && $newCount < $this->count) { $this->pointer = (int) max(0, $this->pointer - ($this->count - $newCount)); } // If the count has increased then the pointer is still // valid. Update the count to include the extra models. $this->count = $newCount; }