Skip to content

Update SelectQuery.php #223

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

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open

Update SelectQuery.php #223

wants to merge 1 commit into from

Conversation

brasizza
Copy link

🔍 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
Copy link
Member

roxblnfk commented Apr 23, 2025

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

Still need to add tests

@brasizza
Copy link
Author

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

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