Skip to content

Commit e2ae9da

Browse files
authored
Merge pull request #64 from Kyslik/ft/left-join
Fix #59; Laravel 5.4
2 parents a43a5a5 + 8b0775d commit e2ae9da

File tree

3 files changed

+47
-64
lines changed

3 files changed

+47
-64
lines changed

.travis.yml

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,8 @@ php:
55
- 7.0
66
- 7.1
77

8-
env:
9-
global:
10-
- setup=basic
11-
12-
matrix:
13-
allow_failures:
14-
- php: 7.1
15-
fast_finish: true
16-
include:
17-
- php: 5.6
18-
env: setup=stable
8+
git:
9+
depth: 3
1910

2011
sudo: false
2112

@@ -28,9 +19,7 @@ before_install:
2819
- travis_retry composer self-update
2920

3021
install:
31-
- if [[ $setup = 'basic' ]]; then travis_retry composer install --no-interaction --prefer-dist --no-suggest; fi
32-
- if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable --no-suggest; fi
33-
- if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable --no-suggest; fi
22+
- travis_retry composer install --no-interaction --prefer-dist --no-suggest;
3423

3524
script: vendor/bin/phpunit
3625

src/ColumnSortable/Sortable.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public function scopeSortable($query, $defaultSortParameters = null)
3939
}
4040
}
4141

42-
4342
/**
4443
* @param \Illuminate\Database\Query\Builder $query
4544
* @param array $sortParameters
@@ -51,18 +50,14 @@ public function scopeSortable($query, $defaultSortParameters = null)
5150
private function queryOrderBuilder($query, array $sortParameters)
5251
{
5352
$model = $this;
54-
5553
list($column, $direction) = $this->parseSortParameters($sortParameters);
56-
5754
if (is_null($column)) {
5855
return $query;
5956
}
60-
6157
$explodeResult = SortableLink::explodeSortParameter($column);
6258
if ( ! empty($explodeResult)) {
6359
$relationName = $explodeResult[0];
6460
$column = $explodeResult[1];
65-
6661
try {
6762
$relation = $query->getRelation($relationName);
6863
$query = $this->queryJoinBuilder($query, $relation);
@@ -71,25 +66,19 @@ private function queryOrderBuilder($query, array $sortParameters)
7166
} catch (\Exception $e) {
7267
throw new ColumnSortableException($relationName, 2, $e);
7368
}
74-
7569
$model = $relation->getRelated();
7670
}
77-
7871
if (method_exists($model, camel_case($column).'Sortable')) {
7972
return call_user_func_array([$model, camel_case($column).'Sortable'], [$query, $direction]);
8073
}
81-
8274
if (isset($model->sortableAs) && in_array($column, $model->sortableAs)) {
8375
$query = $query->orderBy($column, $direction);
8476
} elseif ($this->columnExists($model, $column)) {
8577
$column = $model->getTable().'.'.$column;
8678
$query = $query->orderBy($column, $direction);
8779
}
88-
8980
return $query;
9081
}
91-
92-
9382
/**
9483
* @param array $sortParameters
9584
*
@@ -101,16 +90,12 @@ private function parseSortParameters(array $sortParameters)
10190
if (empty($column)) {
10291
return [null, null];
10392
}
104-
10593
$direction = array_get($sortParameters, 'order', []);
10694
if ( ! in_array($direction, ['asc', 'desc'])) {
10795
$direction = Config::get('columnsortable.default_direction', 'asc');
10896
}
109-
11097
return [$column, $direction];
11198
}
112-
113-
11499
/**
115100
* @param \Illuminate\Database\Query\Builder $query
116101
* @param $relation
@@ -123,29 +108,23 @@ private function queryJoinBuilder($query, $relation)
123108
{
124109
$relatedTable = $relation->getRelated()->getTable();
125110
$parentTable = $relation->getParent()->getTable();
126-
127111
if ($parentTable === $relatedTable) {
128112
$query = $query->from($parentTable.' as parent_'.$parentTable);
129113
$parentTable = 'parent_'.$parentTable;
130114
$relation->getParent()->setTable($parentTable);
131115
}
132-
133116
if ($relation instanceof HasOne) {
134117
$relatedPrimaryKey = $relation->getQualifiedForeignKeyName();
135118
$parentPrimaryKey = $relation->getQualifiedParentKeyName();
136-
137-
return $query->select($parentTable.'.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
119+
return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey);
138120
} elseif ($relation instanceof BelongsTo) {
139121
$relatedPrimaryKey = $relation->getQualifiedOwnerKeyName();
140122
$parentPrimaryKey = $relation->getQualifiedForeignKey();
141-
142-
return $query->select($parentTable.'.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
123+
return $this->formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey);
143124
} else {
144125
throw new \Exception();
145126
}
146127
}
147-
148-
149128
/**
150129
* @param $model
151130
* @param $column
@@ -157,8 +136,6 @@ private function columnExists($model, $column)
157136
return (isset($model->sortable)) ? in_array($column, $model->sortable) :
158137
Schema::hasColumn($model->getTable(), $column);
159138
}
160-
161-
162139
/**
163140
* @param array|string $sort
164141
*
@@ -169,19 +146,30 @@ private function formatToSortParameters($sort)
169146
if (empty($sort)) {
170147
return [];
171148
}
172-
173149
$configDefaultOrder = Config::get('columnsortable.default_direction', 'asc');
174-
175150
if (is_string($sort)) {
176151
return ['sort' => $sort, 'order' => $configDefaultOrder];
177152
}
178-
179153
reset($sort);
180154
$each = each($sort);
181-
182155
return ($each[0] === 0) ? ['sort' => $each[1], 'order' => $configDefaultOrder] : [
183156
'sort' => $each[0],
184157
'order' => $each[1],
185158
];
186159
}
187-
}
160+
/**
161+
* @param $query
162+
* @param $parentTable
163+
* @param $relatedTable
164+
* @param $parentPrimaryKey
165+
* @param $relatedPrimaryKey
166+
*
167+
* @return mixed
168+
*/
169+
private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey)
170+
{
171+
$joinType = Config::get('columnsortable.join_type', 'join');
172+
173+
return $query->select($parentTable.'.*')->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
174+
}
175+
}

src/config/columnsortable.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,56 @@
55
/*
66
spec columns
77
*/
8-
'columns' => [
9-
'alpha' => [
10-
'rows' => ['description', 'email', 'name', 'slug'],
8+
'columns' => [
9+
'alpha' => [
10+
'rows' => ['description', 'email', 'name', 'slug'],
1111
'class' => 'fa fa-sort-alpha',
1212
],
13-
'amount' => [
14-
'rows' => ['amount', 'price'],
15-
'class' => 'fa fa-sort-amount'
13+
'amount' => [
14+
'rows' => ['amount', 'price'],
15+
'class' => 'fa fa-sort-amount',
1616
],
1717
'numeric' => [
18-
'rows' => ['created_at', 'updated_at', 'level', 'id', 'phone_number'],
19-
'class' => 'fa fa-sort-numeric'
18+
'rows' => ['created_at', 'updated_at', 'level', 'id', 'phone_number'],
19+
'class' => 'fa fa-sort-numeric',
2020
],
2121
],
2222

2323
/*
2424
defines icon set to use when sorted data is none above (alpha nor amount nor numeric)
2525
*/
26-
'default_icon_set' => 'fa fa-sort',
26+
'default_icon_set' => 'fa fa-sort',
2727

2828
/*
2929
icon that shows when generating sortable link while column is not sorted
3030
*/
31-
'sortable_icon' => 'fa fa-sort',
31+
'sortable_icon' => 'fa fa-sort',
3232

3333
/*
3434
generated icon is clickable non-clickable (default)
3535
*/
36-
'clickable_icon' => false,
36+
'clickable_icon' => false,
3737

3838
/*
3939
icon and text separator (any string)
4040
in case of 'clickable_icon' => true; separator creates possibility to style icon and anchor-text properly
4141
*/
42-
'icon_text_separator' => ' ',
42+
'icon_text_separator' => ' ',
4343

4444
/*
4545
suffix class that is appended when ascending order is applied
4646
*/
47-
'asc_suffix' => '-asc',
47+
'asc_suffix' => '-asc',
4848

4949
/*
5050
suffix class that is appended when descending order is applied
5151
*/
52-
'desc_suffix' => '-desc',
52+
'desc_suffix' => '-desc',
5353

5454
/*
5555
default anchor class, if value is null none is added
5656
*/
57-
'anchor_class' => null,
57+
'anchor_class' => null,
5858

5959
/*
6060
relation - column separator ex: detail.phone_number means relation "detail" and column "phone_number"
@@ -64,26 +64,32 @@
6464
/*
6565
formatting function applied to name of column, use null to turn formatting off
6666
*/
67-
'formatting_function' => 'ucfirst',
67+
'formatting_function' => 'ucfirst',
6868

6969
/*
7070
inject title parameter in query strings, use null to turn injection off
7171
example: 'inject_title' => 't' will result in ..user/?t="formatted title of sorted column"
7272
*/
73-
'inject_title_as' => null,
73+
'inject_title_as' => null,
7474

7575
/*
7676
allow request modification, when default sorting is set but is not in URI (first load)
7777
*/
78-
'allow_request_modification' => true,
78+
'allow_request_modification' => true,
7979

8080
/*
8181
default order for: $user->sortable('id') usage
8282
*/
83-
'default_direction' => 'asc',
83+
'default_direction' => 'asc',
8484

8585
/*
8686
default order for non-sorted columns
8787
*/
88-
'default_direction_unsorted' => 'asc'
88+
'default_direction_unsorted' => 'asc',
89+
90+
/*
91+
join type: join vs leftJoin (default join)
92+
for more information see https://github.com/Kyslik/column-sortable/issues/59
93+
*/
94+
'join_type' => 'join',
8995
];

0 commit comments

Comments
 (0)