Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update SelectQuery.php#223

Open
brasizza wants to merge 1 commit into
cycle:2.x from
brasizza:2.x
Open

Update SelectQuery.php #223
brasizza wants to merge 1 commit into
cycle:2.x from
brasizza:2.x

Conversation

@brasizza

@brasizza brasizza commented Apr 23, 2025

Copy link
Copy Markdown

🔍 What was changed

Adding fetch() simplifies common one-row use cases, makes intent explicit, and avoids the perf overhead of materialising an unused array.
image

🤔 Why?

  1. Clearer intent
    fetch() makes it obvious we expect one record (or null).
    Code reviewers no longer have to parse an indexing expression to know this.

  2. Avoids unnecessary allocations
    fetchAll() builds a PHP array for every row returned by the database driver.
    When the caller only needs the first row, we:
    allocate and populate an array for all rows,
    immediately destroy most of it,
    pay the GC cost later.

fetch() stops reading after the first result, closes the cursor in the finally, and releases the driver buffer early. This is a measurable win on large result sets and in tight loops.

  1. Reduces boilerplate and error-prone code
    No more [0] ?? null, no more manual casts, no more "did we forget to check the array isn’t empty?" bugs. The new method returns:

an array representing the row (default FETCH_ASSOC), or
false when the result set is empty (mirrors PDO).

  • How was this tested:
    • Tested manually
    • Unit tests added

add fetch() to return just one value when you know that your sql will return just one line that you don't need to check and make transformations
@brasizza brasizza requested review from a team as code owners April 23, 2025 13:48

roxblnfk commented Apr 23, 2025
edited
Loading

Copy link
Copy Markdown
Member

I like the idea. Need to think about the best name for the method: fetch(), fetchOne(), first(), fetchFirst().

Still need to add tests

brasizza reacted with hooray emoji

Copy link
Copy Markdown
Author

I just keep the sema logic with fetchAll and fetch from the pdo

roxblnfk reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /