Skip to content

fix(database): model inspector getSelectFields returns duplicates#2183

Merged
brendt merged 2 commits into
tempestphp:3.xfrom
NeoIsRecursive:fix/model-inspector-get-select-fields-return-duplicates
Jun 23, 2026
Merged

fix(database): model inspector getSelectFields returns duplicates#2183
brendt merged 2 commits into
tempestphp:3.xfrom
NeoIsRecursive:fix/model-inspector-get-select-fields-return-duplicates

Conversation

@NeoIsRecursive

@NeoIsRecursive NeoIsRecursive commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

This causes the mapper to fail "silently".

Example:

// the credential here belongs to the user with id = 2
$passkey = query(Passkey::class)->find(credential_id: $publicKeyCredential->rawId)->first();

// validation logic

// here the user_id would always be 1, signing in the wrong user
$user = query(User::class)->findById($passkey->user_id);

After some digging I found out that the user_id returned from the database as an array [1,1] or [2,2] and so on, which the mapper always map to 1?

And the reason for this is that my model has both a relation and a relation_id field.

final class Passkey
{
    public PrimaryKey $id;

    public User $user;

    public function __construct(
        ...
        public int $user_id,
        ...
    ){}
}

And since the model inspector appends relation ids to the select fields, there came duplicates which made pdo/sqlite(?) return a list for the user_id key.

Anyhow, very simple fix (unless Im missing something) ->unique() on the fields.

@github-actions

Copy link
Copy Markdown

Benchmark Results

Comparison of fix/model-inspector-get-select-fields-return-duplicates against 3.x (3264cdac793d34bdaa25eca3343beeb2d0397383).

Open to see the benchmark results
Benchmark Set Mem. Peak Time Variability
ViewRenderBench(benchControlFlow) - 44.442mb 0.00% 745.470μs -6.65% ±2.01% -15.95%
ViewRenderBench(benchViewComponent) - 69.679mb 0.00% 1.481ms -21.26% ±1.59% -16.69%

Generated by phpbench against commit 8e8fdf0

@brendt brendt merged commit ab45889 into tempestphp:3.x Jun 23, 2026
78 checks passed
@brendt

brendt commented Jun 23, 2026

Copy link
Copy Markdown
Member

Perfect! Thanks :D

@brendt

brendt commented Jun 23, 2026

Copy link
Copy Markdown
Member

Tagged as v3.12.0

@NeoIsRecursive NeoIsRecursive deleted the fix/model-inspector-get-select-fields-return-duplicates branch June 23, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants