/** * Get records from the database. * * @param int $per_page * * @return Collection */ public function get($per_page = 20) { $this->per_page = $per_page; $this->buildQuery(); return $this->query->get(); }
public function build() { if (is_string($this->source) && strpos(" ", $this->source) === false) { //tablename $this->type = "query"; $this->query = $this->table($this->source); $this->total_rows = $this->query->count(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model") || is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->type = "model"; $this->query = $this->source; $this->total_rows = $this->query->count(); } elseif (is_array($this->source)) { $this->type = "array"; $this->total_rows = count($this->source); } //exception //offset and pagination setup/detect $config = array('cid' => $this->cid, 'total_items' => $this->total_rows, 'items_per_page' => $this->per_page, 'num_links' => round($this->num_links / 2), 'hash' => $this->hash, 'url' => $this->url, 'current_page' => $this->current_page); $this->pagination = new \Rapyd\Helpers\Pagination($config); $offset = $this->pagination->offset(); $this->limit($this->per_page, $offset); //build orderby urls $this->orderby_uri_asc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "asc"))->get() . $this->hash; $this->orderby_uri_desc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "desc"))->get() . $this->hash; //detect orderby $orderby = $this->app->url->value("orderby" . $this->cid); if ($orderby) { $this->orderby_field = $orderby[0]; $this->orderby_direction = $orderby[1]; $this->orderby($this->orderby_field, $this->orderby_direction); } //build subset of data switch ($this->type) { case "array": //orderby if (isset($this->orderby)) { list($field, $direction) = $this->orderby; $column = array(); foreach ($this->source as $key => $row) { $column[$key] = $row[$field]; } if ($direction == "asc") { array_multisort($column, SORT_ASC, $this->source); } else { array_multisort($column, SORT_DESC, $this->source); } } //limit-offset if (isset($this->limit)) { $this->source = array_slice($this->source, $this->limit[1], $this->limit[0]); } $this->data = $this->source; break; case "query": case "model": //orderby if (isset($this->orderby)) { $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]); } //limit-offset if (isset($this->limit)) { $this->query = $this->query->skip($this->pagination->offset())->take($this->per_page); } $this->data = $this->query->get(); break; } return $this; }
/** * Get the hydrated models without eager loading. * * @param array $columns * @return \Illuminate\Database\Eloquent\Model[] */ public function getModels($columns = ['*']) { $results = $this->query->get($columns); $connection = $this->model->getConnectionName(); return $this->model->hydrate($results, $connection)->all(); }
/** * @param Builder|EloquentBuilder $data * @return Traversable */ protected function afterOperations($data) { if ($data instanceof EloquentBuilder) { return $data->get(); } elseif ($data instanceof Builder) { return new ArrayIterator($data->get()); } throw new RuntimeException('Unsupported type of data source.'); }
public function totalCount() { if ($this->options['distinctCountGroup'] && count($this->originalBuilder->groups) == 1) { $this->originalBuilder->groups = null; } if ($this->options['searchWithAlias']) { $cnt = count($this->originalBuilder->get()); } else { $cnt = $this->originalBuilder->count(); } return $cnt; }
/** * Find data by multiple fields * * @param array $where * @param array $columns * @return mixed|Model */ public function findWhere(array $where, $columns = ['*']) { return $this->wrap(function ($where, $columns = ['*']) { $this->applyWhere($where); return $this->model->get($columns); }, new Action(__METHOD__, func_get_args(), Action::READ)); }
/** * Gets results from prepared query * * @return null */ protected function getResult() { if ($this->query_type == 'eloquent') { $this->result_object = $this->query->get(); $this->result_array = $this->result_object->toArray(); } else { $this->result_object = $this->query->get(); $this->result_array = array_map(function ($object) { return (array) $object; }, $this->result_object); } if ($this->dataFullSupport) { $walk = function ($value, $key, $prefix = null) use(&$walk, &$result_array) { $key = !is_null($prefix) ? $prefix . "." . $key : $key; if (is_array($value)) { array_walk($value, $walk, $key); } else { $result_array = Arr::add($result_array, $key, $value); } }; $result_array = array(); array_walk($this->result_array, $walk); $this->result_array = $result_array; } }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = array('*')) { if ($this->model instanceof Model) { $this->model->filterQuery($this, 'after'); } return parent::get($columns); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = ['*']) { if (null !== $this->cacheMinutes) { return $this->getCached($columns); } return parent::get($columns); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = array('*')) { if (!is_null($this->cacheMinutes)) { return $this->getCached($columns); } return parent::get($columns); }
/** * flush events, build pagination and sort items * * @return $this */ public function build() { BurpEvent::flush('dataset.sort'); BurpEvent::flush('dataset.page'); $this->paginator = Paginator::make($this->total_rows, $this->per_page, $this->page); $offset = $this->paginator->offset(); $this->limit($this->per_page, $offset); if (is_array($this->source)) { //orderby if (isset($this->orderby)) { list($field, $direction) = $this->orderby; array_orderby($this->source, $field, $direction); } //limit-offset if (isset($this->limit)) { $this->source = array_slice($this->source, $this->limit[1], $this->limit[0]); } $this->data = $this->source; } else { //orderby if (isset($this->orderby)) { $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]); } //limit-offset if (isset($this->per_page)) { $this->query = $this->query->skip($offset)->take($this->per_page); } $this->data = $this->query->get(); } return $this; }
/** * Return all of the results. * * @return array * @throws TableNotSetException */ public function results() { if (!$this->query) { throw new TableNotSetException("You must set a database table to get results from."); } return $this->query->get($this->select); }
public function get($columns = array('*')) { for ($n = 0; $n < count($columns); $n++) { $columns[$n] = is_string($columns[$n]) ? snake_case($columns[$n]) : $columns[$n]; } return parent::get($columns); // TODO: Change the autogenerated stub }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = ['*']) { $cacheKey = $this->generateCacheKey(); if (null === ($results = $this->cache->tags($this->cacheTag)->get($cacheKey))) { $results = parent::get($columns); $this->cache->tags($this->cacheTag)->forever($cacheKey, $results); } return $results; }
/** * get list * * @param array $columns get columns list * @return array|static[] */ public function get(array $columns = ['*']) { if ($this->dynamic === false) { return $this->query->get($columns); } if ($this->proxy === true) { $this->query = $this->getProxyManager()->get($this->query); } return $this->query->get($columns); }
/** * Returns HTML with resource table * * @return string * @throws CollectionException */ public function make() { if (empty($this->_columns)) { throw new CollectionException('At least one column is required to generate a resource table.'); } // Prepare builder object before calling Table $this->_prepareBuilder(); // Finally execute prepared query builder $items = $this->_builder->get(); return with(new Table($items, ['collection_generator' => $this, 'columns' => $this->_columns, 'per_page' => $this->_perPage, 'paginate' => $this->_paginate, 'paginator_presenter' => $this->_getPaginatorPresenter($items), 'view_name' => $this->_viewName, 'filter' => $this->_filter, 'extra' => $this->_extraViewData]))->make(); }
/** * This method is overloaded from the base Elquent builder, so we can first see if the cached models already exist, and we can then bypass checking the database * entirely. * * @access public * @param array $columns * @return array */ public function get($columns = array('*')) { $cachedModels = $this->findCachedModels(); if (!empty($cachedModels)) { return $cachedModels; } $results = parent::get($columns); foreach ($results as $result) { SquirrelCache::remember($this->sourceModel, (array) $result); } return $results; }
/** * Execute the query as a "select" statement. * All Cached results will be flushed after every CUD operations * * @param array $columns * @return array|static[] */ public function get($columns = ['*']) { $cacheKey = $this->generateCacheKey(); if (null === ($results = $this->cache->tags($this->cacheTag)->get($cacheKey))) { $results = parent::get($columns); if ($this->isTimeAwareQuery) { // Cache results for $cacheLifeTime minutes $this->cache->tags($this->cacheTag)->put($cacheKey, $results, $this->cacheLifeTime); } else { // Cache results forever $this->cache->tags($this->cacheTag)->forever($cacheKey, $results); } } return $results; }
/** * Get the hydrated models without eager loading. * * @param array $columns * @return array|static[] */ public function getModels($columns = array('*')) { // First, we will simply get the raw results from the query builders which we // can use to populate an array with Eloquent models. We will pass columns // that should be selected as well, which are typically just everything. $results = $this->query->get($columns); $connection = $this->model->getConnectionName(); $models = array(); // Once we have the results, we can spin through them and instantiate a fresh // model instance for each records we retrieved from the database. We will // also set the proper connection name for the model after we create it. foreach ($results as $result) { $models[] = $model = $this->model->newFromBuilder($result); $model->setConnection($connection); } return $models; }
/** * Возвращает коллекцию в виде пагинации * * @param int $page * @param int $limit */ public function pagination($page, $limit = 10) { /** * @var \Illuminate\Support\Collection $data */ $countTotal = $this->_query->count(); $this->_query->skip($limit * $page - $limit); $this->_query->limit($limit); $data = collect(); foreach ($this->_query->get() as $key => $instance) { $_listRow = []; foreach ($this->columns->getColumns() as $column) { $_listRow[$column->getKey()] = $column->getValues($instance); } $buttons = $this->filterAction($instance); if (count($buttons)) { $_listRow = array_merge($_listRow, [GridColumn::ACTION_NAME => implode('', $buttons)]); } $data->offsetSet($key, $_listRow); } return new \Illuminate\Pagination\LengthAwarePaginator($data, $countTotal, $limit, $page, ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath(), 'pageName' => 'page']); }
/** * Return the filtered, ordered and paginated rows from the crud's custom query * * @param Builder $query * @param int $start * @param int $length * @param array $filters * @param null $order * @return array */ public static function getFilteredQueryListRows(Builder $query, $start, $length, $filters = [], $order = null) { // Get the total count of rows with no filters and pagination $countTotal = $query->count(); // Check if any filters were submitted if ($filters) { foreach ($filters as $filterName => $filterValue) { // Check if there is any filter operator in the field string $operation = SQL::findOperationWhere($filterValue); $query->where($filterName, $operation['operation'], $operation['text']); } } // Get the total count of rows filtered, but without pagination $countFiltered = $query->count(); // Execute pagination $query->skip($start)->take($length); // Check if an order by was submitted if ($order) { $query->orderBy($order['name'], $order['dir']); } return ['rows' => $query->get(), 'count_filtered' => $countFiltered, 'count_total' => $countTotal]; }
/** * 查找受影响的 ID,清空相关行级缓存 */ private function flushAffectingRowCache() { $keyName = $this->model->primaryKey(); $toDeleteRows = parent::get(); $ids = []; foreach ($toDeleteRows as $row) { $ids[] = $row->{$keyName}; } if ($ids) { $cacheKeys = $this->buildRowCacheKey($ids); $cache = $this->getCache(); $cache->del(array_values($cacheKeys)); } }
public function build() { if (is_string($this->source) && strpos(" ", $this->source) === false) { //tablename $this->type = "query"; $this->query = $this->table($this->source); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model")) { $this->type = "model"; $this->query = $this->source; $this->key = $this->source->getKeyName(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->type = "model"; $this->query = $this->source; $this->key = $this->source->getModel()->getKeyName(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Query\\Builder")) { $this->type = "model"; $this->query = $this->source; } elseif (is_a($this->source, "\\Zofe\\Rapyd\\DataFilter\\DataFilter")) { $this->type = "model"; $this->query = $this->source->query; if (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Model")) { $this->key = $this->query->getKeyName(); } elseif (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->key = $this->query->getModel()->getKeyName(); } } elseif (is_array($this->source)) { $this->type = "array"; } else { throw new DataSetException(' "source" must be a table name, an eloquent model or an eloquent builder. you passed: ' . get_class($this->source)); } //build orderby urls $this->orderby_uri_asc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "-field-")->get() . $this->hash; $this->orderby_uri_desc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "--field-")->get() . $this->hash; //detect orderby $orderby = $this->url->value("ord" . $this->cid); if ($orderby) { $this->orderby_field = ltrim($orderby, "-"); $this->orderby_direction = $orderby[0] === "-" ? "desc" : "asc"; if ($this->canOrderby($this->orderby_field)) { $this->orderBy($this->orderby_field, $this->orderby_direction); } } //build subset of data switch ($this->type) { case "array": //orderby if (isset($this->orderby)) { list($field, $direction) = $this->orderby; $column = array(); foreach ($this->source as $key => $row) { $column[$key] = is_object($row) ? $row->{$field} : $row[$field]; } if ($direction == "asc") { array_multisort($column, SORT_ASC, $this->source); } else { array_multisort($column, SORT_DESC, $this->source); } } $limit = $this->limit ? $this->limit : 100000; $current_page = $this->url->value('page' . $this->cid, 0); $offset = max($current_page - 1, 0) * $limit; $this->data = array_slice($this->source, $offset, $limit); $this->paginator = new LengthAwarePaginator($this->data, count($this->source), $limit, $current_page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => "page" . $this->cid]); break; case "query": case "model": //orderby if (isset($this->orderby)) { $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]); } //limit-offset if (isset($this->limit)) { $this->paginator = $this->query->paginate($this->limit, ['*'], 'page' . $this->cid); $this->data = $this->paginator; } else { $this->data = $this->query->get(); } break; } return $this; }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = array()) { return parent::get($columns); }
/** * @param Builder $query * @return array */ protected static function makeWidgetsList(Builder $query) { $widgets = []; foreach ($query->get() as $id => $widget) { $widgets[$widget->id] = static::makeWidget($widget); } return $widgets; }
/** * Execute the query as a "select" statement. * * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|static[] */ public function get($columns = array('*')) { if (!$this->applyTagFilters()) { if (!empty($this->aggregate)) { return 0; } return $this->model->newCollection(); } if (!empty($this->aggregate)) { return parent::get($columns); } else { $results = parent::get(array('t.xref_id')); } if (empty($results)) { return $this->model->newCollection(); } $xref_ids = array(); foreach ($results as $result) { if (!isset($result->xref_id)) { continue; } $xref_ids[] = $result->xref_id; } if (empty($xref_ids)) { return $this->model->newCollection(); } $key = $this->model->getKeyName(); $models = $this->model->newQuery()->whereIn($key, $xref_ids)->get(); $models->sortBy(function ($model) use($xref_ids, $key) { foreach ($xref_ids as $idx => $i) { if ($model->{$key} == $i) { return $idx; } } return 0; }); if (!empty($this->relations)) { $models->load($this->relations); } return $models; }
/** * 处理上方所有条件后, 执行查询语句, 返回结果集 * * @param array $columns 默认获取全部字段 * @return mixed */ protected function selectQuery(array $columns = []) : \Illuminate\Support\Collection { return $this->original->get($columns); }
/** * @return mixed */ public function results() { return $this->query->get(); }
/** * Execute the query as a "select" statement. * * @param array $columns * @return array|static[] */ public function get($columns = []) { return parent::get($columns); }
/** * Get the Closure callback used when caching queries. * * @param array $columns * @return \Closure */ protected function getCacheCallback($columns) { return function () use($columns) { return parent::get($columns); }; }