Skip to content

Commit 2e7013b

Browse files
committed
Fix Unique::getPaginatedQuery to call to addSelect instead of select during the pre-paginated query
1 parent 4dda14f commit 2e7013b

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## v4.1.19
9+
- Fix `Unique::getPaginatedQuery` to call to `addSelect` instead of `select` during the pre-paginated query
10+
811
## v4.1.18
912
- Bakery setup wizard for SMTP config + separate SMTP setup in it's own command (https://github.com/userfrosting/UserFrosting/issues/874)
1013
- Update Italian translations (https://github.com/userfrosting/UserFrosting/pull/875)

app/defines.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace UserFrosting;
44

55
// Some standard defines
6-
define('UserFrosting\VERSION', '4.1.18');
6+
define('UserFrosting\VERSION', '4.1.19');
77
define('UserFrosting\DS', '/');
88
define('UserFrosting\PHP_MIN_VERSION', '5.6');
99
define('UserFrosting\DEBUG_CONFIG', false);

app/sprinkles/core/src/Database/Relations/Concerns/Unique.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public function getPaginatedQuery(Builder $query, $limit = null, $offset = null)
277277

278278
// Apply an additional scope to override any selected columns in other global scopes
279279
$uniqueIdScope = function ($subQuery) use ($relatedKeyName) {
280-
$subQuery->select($relatedKeyName)
280+
$subQuery->addSelect($relatedKeyName)
281281
->groupBy($relatedKeyName);
282282
};
283283

app/sprinkles/core/tests/Integration/DatabaseTests.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function tearDown()
119119
$this->schema($this->schemaName)->drop('tasks');
120120
$this->schema($this->schemaName)->drop('locations');
121121
$this->schema($this->schemaName)->drop('assignments');
122+
$this->schema($this->schemaName)->drop('jobs');
122123

123124
Relation::morphMap([], false);
124125
}
@@ -658,6 +659,35 @@ public function testBelongsToManyThroughPaginated()
658659
$this->assertEquals(2, $paginatedPermissions->count());
659660
}
660661

662+
/**
663+
* Test the ability of a BelongsToManyThrough relationship to retrieve and count paginated queries,
664+
* when we need to reference a virtual/computed column (for example in a sort).
665+
*/
666+
public function testBelongsToManyThroughPaginatedWithOrderByAggregateColumn()
667+
{
668+
$this->generateRolesWithPermissions();
669+
670+
$user = EloquentTestUser::create(['name' => 'David']);
671+
$user->roles()->attach([1,2]);
672+
673+
// If the paginated query is being ordered correctly by including the `roles_count` computed column,
674+
// Then `uri_spit_acid` should appear first. If not, then the results will not be ordered and the `uri_harvest`
675+
// result will be returned, in accordance with the default database order.
676+
$paginatedPermissions = $user->permissions()->withCount('roles')->orderBy('roles_count', 'desc')->take(1)->offset(0);
677+
678+
$this->assertEquals([
679+
[
680+
'id' => 2,
681+
'slug' => 'uri_spit_acid',
682+
'roles_count' => 3,
683+
'pivot' => [
684+
'user_id' => 1,
685+
'permission_id' => 2
686+
]
687+
]
688+
], $paginatedPermissions->get()->toArray());
689+
}
690+
661691
/**
662692
* Test the ability of a BelongsToManyThrough relationship to retrieve structured data on a single model or set of models,
663693
* eager loading the "via" models at the same time.

0 commit comments

Comments
 (0)