Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: RelationshipJoiner raw & sub-query orders #15261

Merged
merged 9 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/support/src/Services/RelationshipJoiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -91,15 +92,32 @@ function (array $where) use ($relationship): bool {

/** @phpstan-ignore-next-line */
foreach (($relationshipQuery->getQuery()->orders ?? []) as $order) {
if (! array_key_exists('column', $order)) {
// Regular orders: { column: string, direction: 'asc' | 'desc' }
// Sub-query orders: { column: Illuminate\Database\Query\Expression, direction: 'asc' | 'desc' }
// Raw orders: { type: 'Raw', sql: string }
if (! array_key_exists('column', $order) && ! array_key_exists('sql', $order)) {
continue;
}

if (str($order['column'])->startsWith("{$relationshipQuery->getModel()->getTable()}.")) {
$columnValue = $order['column'] ?? new Expression($order['sql']);

if (
$columnValue instanceof Expression
&& str($columnValue->getValue($relationship->getGrammar()))->contains('?')
) {
// Heuristic to determine if the expression contains (a) binding(s), if so, as of
// yet we cannot reliably determine (which) bindings are used in the expression.
continue;
}

if (
str($columnValue instanceof Expression ? $columnValue->getValue($relationship->getGrammar()) : $columnValue)
->startsWith("{$relationshipQuery->getModel()->getTable()}.")
) {
continue;
}

$relationshipQuery->addSelect($order['column']);
$relationshipQuery->addSelect($columnValue);
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/database/migrations/create_team_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('team_user', function (Blueprint $table): void {
$table->id();
$table->foreignId('team_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->string('role')->nullable();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('team_user');
}
};
6 changes: 6 additions & 0 deletions tests/src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand Down Expand Up @@ -36,6 +37,11 @@ public function posts(): HasMany
return $this->hasMany(Post::class, 'author_id');
}

public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}

protected static function newFactory()
{
return UserFactory::new();
Expand Down
88 changes: 88 additions & 0 deletions tests/src/Support/Services/RelationshipJoinerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use Filament\Support\Services\RelationshipJoiner;
use Filament\Tests\Models\Team;
use Filament\Tests\Models\User;
use Filament\Tests\TestCase;
use Illuminate\Database\Query\Expression;

uses(TestCase::class);

it('can prepare query for no constraints for a BelongsToMany relationship', function () {
$user = User::factory()->create();

expect($user->teams()->toBase())
->distinct->toBeFalse()
->getColumns()->toBe([])
->orders->toBeNull();

$preparedQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints($user->teams());

expect($preparedQuery->toBase())
->distinct->toBeTrue()
->getColumns()->toBe(['teams.*'])
->orders->toBeNull();

$preparedQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
$user
->teams()
->orderBy('id')
->orderBy((new Team)->qualifyColumn('name'))
->orderBy('team_user.role')
);

expect($preparedQuery->toBase())
->distinct->toBeTrue()
->getColumns()->toBe([
(new Team)->qualifyColumn('*'), // Default select...
'id', // Select without a qualified table also included just to be sure...
// Select for `team.name` not included as that is already included in the `team.*`...
'team_user.role', // Select for a qualitified other table included...
])
->orders->toBe([
[
'column' => 'id',
'direction' => 'asc',
],
[
'column' => 'teams.name',
'direction' => 'asc',
],
[
'column' => 'team_user.role',
'direction' => 'asc',
],
]);

$preparedQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
$user->teams()->orderByRaw("CASE WHEN role = 'admin' THEN 1 ELSE 2 END")
);

expect($preparedQuery->toBase())
->distinct->toBeTrue()
->getColumns()->toBe([
(new Team)->qualifyColumn('*'),
"CASE WHEN role = 'admin' THEN 1 ELSE 2 END", // Select added from `orderByRaw`...
])
->orders->toBe([
[
'type' => 'Raw',
'sql' => "CASE WHEN role = 'admin' THEN 1 ELSE 2 END",
],
]);

$preparedQuery = app(RelationshipJoiner::class)->prepareQueryForNoConstraints(
$user->teams()->orderBy(new Expression("CASE WHEN role = 'some_other_role' THEN 1 ELSE 2 END"))
);

expect($preparedQuery->toBase())
->distinct->toBeTrue()
->getColumns()->toBe([
(new Team)->qualifyColumn('*'),
"CASE WHEN role = 'some_other_role' THEN 1 ELSE 2 END", // Select added from `orderByRaw`...
])
->orders->toHaveCount(1)
->and($preparedQuery->toBase()->orders[0])
->column->getValue($user->teams()->getGrammar())->toBe("CASE WHEN role = 'some_other_role' THEN 1 ELSE 2 END")
->direction->toBe('asc');
});
Loading