Exemple #1
0
 /**
  * If column name could not be resolved then use primary key.
  *
  * @return string
  */
 protected function getPrimaryKeyName()
 {
     if ($this->isEloquent()) {
         return $this->query->getModel()->getKeyName();
     }
     return 'id';
 }
 public function generateFromRelations()
 {
     $this->parent = $this->query->getModel();
     foreach (explode('.', $this->relations) as $index => $relation) {
         $this->index = $index;
         $this->relation = call_user_func(array($this->parent, $relation));
         $this->related = $this->relation->getRelated();
         if ($this->nested) {
             if (!array_key_exists($this->index, $this->ons)) {
                 $this->ons[$this->index] = [];
             }
             if (!array_key_exists($this->index, $this->conditions)) {
                 $this->conditions[$this->index] = [];
             }
         }
         if ($this->relation instanceof BelongsTo) {
             $this->generateJoinFromBelongsTo($this->relation);
         } elseif ($this->relation instanceof BelongsToMany) {
             $this->generateJoinFromBelongsToMany($this->relation);
         } elseif ($this->relation instanceof HasOneOrMany) {
             $this->generateJoinFromHasOneOrMany($this->relation);
         }
         $this->parent = $this->relation->getRelated();
     }
     if ($this->addNullCondition) {
         if ($this->relation instanceof BelongsTo) {
             $this->generateNullConditionFromBelongsTo($this->relation);
         } elseif ($this->relation instanceof BelongsToMany) {
             $this->generateNullConditionFromBelongsToMany();
         } elseif ($this->relation instanceof HasOneOrMany) {
             $this->generateNullConditionFromHasOneOrMany($this->relation);
         }
     }
 }
Exemple #3
0
 /**
  * Create new joiner instance.
  *
  * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query
  * @param  \Illuminate\Database\Eloquent\Model $model
  * @return \Sofa\Eloquence\Relations\Joiner
  */
 public static function make($query, Model $model = null)
 {
     if ($query instanceof EloquentBuilder) {
         $model = $query->getModel();
         $query = $query->getQuery();
     }
     return new Joiner($query, $model);
 }
 /**
  * Counts current query
  *
  * @param string $count variable to store to 'count_all' for iTotalRecords, 'display_all' for iTotalDisplayRecords
  *
  * @return null
  */
 protected function count($count = 'count_all')
 {
     //Get columns to temp var.
     if ($this->query_type == 'eloquent') {
         $query = $this->query->getQuery();
         $connection = $this->query->getModel()->getConnection()->getName();
     } else {
         $query = $this->query;
         $connection = $query->getConnection()->getName();
     }
     // if its a normal query ( no union ) replace the select with static text to improve performance
     $countQuery = clone $query;
     if (!preg_match('/UNION/i', $countQuery->toSql())) {
         $countQuery->select(DB::raw("'1' as row"));
         // if query has "having" clause add select columns
         if ($countQuery->havings) {
             foreach ($countQuery->havings as $having) {
                 if (isset($having['column'])) {
                     $countQuery->addSelect($having['column']);
                 } else {
                     // search filter_columns for query string to get column name from an array key
                     $found = false;
                     foreach ($this->filter_columns as $column => $filter) {
                         if ($filter['parameters'][0] == $having['sql']) {
                             $found = $column;
                             break;
                         }
                     }
                     // then correct it if it's an alias and add to columns
                     if ($found !== false) {
                         foreach ($this->columns as $col) {
                             $arr = preg_split('/ as /i', $col);
                             if (isset($arr[1]) && $arr[1] == $found) {
                                 $found = $arr[0];
                                 break;
                             }
                         }
                         $countQuery->addSelect($found);
                     }
                 }
             }
         }
     }
     // Clear the orders, since they are not relevant for count
     $countQuery->orders = null;
     $this->{$count} = DB::connection($connection)->table(DB::raw('(' . $countQuery->toSql() . ') AS count_row_table'))->setBindings($countQuery->getBindings())->count();
 }
 /**
  * Метод фильтрует строки по умолчанию для любой модели
  */
 protected function _prepareColumns()
 {
     $this->columns(function ($grid) {
         /** @var  \Assurrussa\GridView\Support\GridColumns $grid */
         $model = $this->_query->getModel();
         $lists = \Schema::getColumnListing($model->getTable());
         $columns = [];
         foreach ($lists as $key => $list) {
             $columns[] = $grid->column()->setKey($list)->setValue($list)->setDate(true)->setSort(true);
         }
         $columns[] = $grid->column()->setKeyAction()->setActions(function ($data) use($grid) {
             $pathNameForModel = strtolower(str_plural(camel_case(class_basename($data))));
             $buttons = [];
             $buttons[] = $grid->button()->setAction('delete')->setLabel('deleted')->setRoute('delete', [$pathNameForModel, $data->id])->setIcon('fa-cancel');
             $buttons[] = $grid->button()->setAction('show')->setLabel('show')->setRoute('show', [$pathNameForModel, $data->id])->setIcon('fa-show')->setHandler(function ($data) {
                 return false;
             });
             $buttons[] = $grid->button()->setAction('edit')->setLabel('edit')->setRoute('edit', [$pathNameForModel, $data->id])->setIcon('fa-edit');
             return $buttons;
         });
         return $grid->setColumns($columns);
     });
 }
Exemple #6
0
 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;
 }
 /**
  * Return a new search criteria.
  *
  * @param  string $term
  * @return SearchCriteria
  */
 public function search($term)
 {
     return new SearchCriteria($this->query->getModel()->search($term), $this->query->getModel());
 }
 /**
  * @param EloquentBuilder|QueryBuilder|Model $query
  * @param string $column
  * @param EloquentBuilder|QueryBuilder|Model $subQuery
  * @param string $subQueryColumn
  *
  * @return QueryBuilder
  */
 public function scopeWhereInSubQuery($query, $column, $subQuery, $subQueryColumn)
 {
     if (!Str::contains($column, '.')) {
         /** @var Model|BetterEloquentTrait $model */
         $model = $query->getModel();
         $column = $model->getField($column);
     }
     $subQuery = $subQuery->toSubQuery($subQueryColumn, true);
     return $query->whereIn($column, $subQuery);
 }