From c2b833c199cc2ee368df8912d93c18db8aa11d41 Mon Sep 17 00:00:00 2001 From: Martin Kiesel Date: Thu, 31 Aug 2017 14:58:13 +0200 Subject: [PATCH 1/2] Fix #59; Lravel 5.4 --- src/ColumnSortable/Sortable.php | 48 +++++++++++++-------------------- src/config/columnsortable.php | 46 +++++++++++++++++-------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/src/ColumnSortable/Sortable.php b/src/ColumnSortable/Sortable.php index dbb281f..85452ff 100755 --- a/src/ColumnSortable/Sortable.php +++ b/src/ColumnSortable/Sortable.php @@ -39,7 +39,6 @@ public function scopeSortable($query, $defaultSortParameters = null) } } - /** * @param \Illuminate\Database\Query\Builder $query * @param array $sortParameters @@ -51,18 +50,14 @@ public function scopeSortable($query, $defaultSortParameters = null) private function queryOrderBuilder($query, array $sortParameters) { $model = $this; - list($column, $direction) = $this->parseSortParameters($sortParameters); - if (is_null($column)) { return $query; } - $explodeResult = SortableLink::explodeSortParameter($column); if ( ! empty($explodeResult)) { $relationName = $explodeResult[0]; $column = $explodeResult[1]; - try { $relation = $query->getRelation($relationName); $query = $this->queryJoinBuilder($query, $relation); @@ -71,25 +66,19 @@ private function queryOrderBuilder($query, array $sortParameters) } catch (\Exception $e) { throw new ColumnSortableException($relationName, 2, $e); } - $model = $relation->getRelated(); } - if (method_exists($model, camel_case($column).'Sortable')) { return call_user_func_array([$model, camel_case($column).'Sortable'], [$query, $direction]); } - if (isset($model->sortableAs) && in_array($column, $model->sortableAs)) { $query = $query->orderBy($column, $direction); } elseif ($this->columnExists($model, $column)) { $column = $model->getTable().'.'.$column; $query = $query->orderBy($column, $direction); } - return $query; } - - /** * @param array $sortParameters * @@ -101,16 +90,12 @@ private function parseSortParameters(array $sortParameters) if (empty($column)) { return [null, null]; } - $direction = array_get($sortParameters, 'order', []); if ( ! in_array($direction, ['asc', 'desc'])) { $direction = Config::get('columnsortable.default_direction', 'asc'); } - return [$column, $direction]; } - - /** * @param \Illuminate\Database\Query\Builder $query * @param $relation @@ -123,29 +108,23 @@ private function queryJoinBuilder($query, $relation) { $relatedTable = $relation->getRelated()->getTable(); $parentTable = $relation->getParent()->getTable(); - if ($parentTable === $relatedTable) { $query = $query->from($parentTable.' as parent_'.$parentTable); $parentTable = 'parent_'.$parentTable; $relation->getParent()->setTable($parentTable); } - if ($relation instanceof HasOne) { $relatedPrimaryKey = $relation->getQualifiedForeignKeyName(); $parentPrimaryKey = $relation->getQualifiedParentKeyName(); - - return $query->select($parentTable.'.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey); + return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey); } elseif ($relation instanceof BelongsTo) { $relatedPrimaryKey = $relation->getQualifiedOwnerKeyName(); $parentPrimaryKey = $relation->getQualifiedForeignKey(); - - return $query->select($parentTable.'.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey); + return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey); } else { throw new \Exception(); } } - - /** * @param $model * @param $column @@ -157,8 +136,6 @@ private function columnExists($model, $column) return (isset($model->sortable)) ? in_array($column, $model->sortable) : Schema::hasColumn($model->getTable(), $column); } - - /** * @param array|string $sort * @@ -169,19 +146,30 @@ private function formatToSortParameters($sort) if (empty($sort)) { return []; } - $configDefaultOrder = Config::get('columnsortable.default_direction', 'asc'); - if (is_string($sort)) { return ['sort' => $sort, 'order' => $configDefaultOrder]; } - reset($sort); $each = each($sort); - return ($each[0] === 0) ? ['sort' => $each[1], 'order' => $configDefaultOrder] : [ 'sort' => $each[0], 'order' => $each[1], ]; } -} + /** + * @param $query + * @param $parentTable + * @param $relatedTable + * @param $parentPrimaryKey + * @param $relatedPrimaryKey + * + * @return mixed + */ + private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey) + { + $joinType = Config::get('columnsortable.join_type', 'join'); + + return $query->select($parentTable.'.*')->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey); + } +} \ No newline at end of file diff --git a/src/config/columnsortable.php b/src/config/columnsortable.php index 7bd4cc3..bba4f79 100755 --- a/src/config/columnsortable.php +++ b/src/config/columnsortable.php @@ -5,56 +5,56 @@ /* spec columns */ - 'columns' => [ - 'alpha' => [ - 'rows' => ['description', 'email', 'name', 'slug'], + 'columns' => [ + 'alpha' => [ + 'rows' => ['description', 'email', 'name', 'slug'], 'class' => 'fa fa-sort-alpha', ], - 'amount' => [ - 'rows' => ['amount', 'price'], - 'class' => 'fa fa-sort-amount' + 'amount' => [ + 'rows' => ['amount', 'price'], + 'class' => 'fa fa-sort-amount', ], 'numeric' => [ - 'rows' => ['created_at', 'updated_at', 'level', 'id', 'phone_number'], - 'class' => 'fa fa-sort-numeric' + 'rows' => ['created_at', 'updated_at', 'level', 'id', 'phone_number'], + 'class' => 'fa fa-sort-numeric', ], ], /* defines icon set to use when sorted data is none above (alpha nor amount nor numeric) */ - 'default_icon_set' => 'fa fa-sort', + 'default_icon_set' => 'fa fa-sort', /* icon that shows when generating sortable link while column is not sorted */ - 'sortable_icon' => 'fa fa-sort', + 'sortable_icon' => 'fa fa-sort', /* generated icon is clickable non-clickable (default) */ - 'clickable_icon' => false, + 'clickable_icon' => false, /* icon and text separator (any string) in case of 'clickable_icon' => true; separator creates possibility to style icon and anchor-text properly */ - 'icon_text_separator' => ' ', + 'icon_text_separator' => ' ', /* suffix class that is appended when ascending order is applied */ - 'asc_suffix' => '-asc', + 'asc_suffix' => '-asc', /* suffix class that is appended when descending order is applied */ - 'desc_suffix' => '-desc', + 'desc_suffix' => '-desc', /* default anchor class, if value is null none is added */ - 'anchor_class' => null, + 'anchor_class' => null, /* relation - column separator ex: detail.phone_number means relation "detail" and column "phone_number" @@ -64,26 +64,32 @@ /* formatting function applied to name of column, use null to turn formatting off */ - 'formatting_function' => 'ucfirst', + 'formatting_function' => 'ucfirst', /* inject title parameter in query strings, use null to turn injection off example: 'inject_title' => 't' will result in ..user/?t="formatted title of sorted column" */ - 'inject_title_as' => null, + 'inject_title_as' => null, /* allow request modification, when default sorting is set but is not in URI (first load) */ - 'allow_request_modification' => true, + 'allow_request_modification' => true, /* default order for: $user->sortable('id') usage */ - 'default_direction' => 'asc', + 'default_direction' => 'asc', /* default order for non-sorted columns */ - 'default_direction_unsorted' => 'asc' + 'default_direction_unsorted' => 'asc', + + /* + join type: join vs leftJoin (default join) + for more information see https://github.com/Kyslik/column-sortable/issues/59 + */ + 'join_type' => 'join', ]; From 8b0775def194e619639d69d3e66f43f3d2fee4a7 Mon Sep 17 00:00:00 2001 From: Martin Kiesel Date: Thu, 31 Aug 2017 15:00:42 +0200 Subject: [PATCH 2/2] Update .travis file --- .travis.yml | 17 +++-------------- src/ColumnSortable/Sortable.php | 2 +- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4e7472f..62df10f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,17 +5,8 @@ php: - 7.0 - 7.1 -env: - global: - - setup=basic - -matrix: - allow_failures: - - php: 7.1 - fast_finish: true - include: - - php: 5.6 - env: setup=stable +git: + depth: 3 sudo: false @@ -28,9 +19,7 @@ before_install: - travis_retry composer self-update install: - - if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist --no-suggest; fi - - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi - - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi + - travis_retry composer install --no-interaction --prefer-dist --no-suggest; script: vendor/bin/phpunit diff --git a/src/ColumnSortable/Sortable.php b/src/ColumnSortable/Sortable.php index 85452ff..4b858ed 100755 --- a/src/ColumnSortable/Sortable.php +++ b/src/ColumnSortable/Sortable.php @@ -169,7 +169,7 @@ private function formatToSortParameters($sort) private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey) { $joinType = Config::get('columnsortable.join_type', 'join'); - + return $query->select($parentTable.'.*')->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey); } } \ No newline at end of file