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

FETCH_KEY_PAIR / FETCH_UNIQUE / FETCH_GROUP fetch modes #14

Open
bfinlay opened this issue May 28, 2019 · 2 comments
Open

FETCH_KEY_PAIR / FETCH_UNIQUE / FETCH_GROUP fetch modes #14

bfinlay opened this issue May 28, 2019 · 2 comments

Comments

@bfinlay
Copy link

bfinlay commented May 28, 2019

In writing framework benchmark, one of the test cases required data that would have been more conveniently fetched using the FETCH_KEY_PAIR mode. I worked around it by using array_column.

It would be more convenient to either specify the fetch mode as an optional parameter to select or have additional methods (like selectColumn).

public static function fortunes(App $app) {
        $fortunes = Controller::getDb()->select(
            'SELECT * FROM Fortune'
        );
        $fortunes = array_column($fortunes, 'message', 'id');
        $fortunes[0] = 'Additional fortune added at request time.';
        asort($fortunes);
        echo $app->view('fortunes.html.twig', ['fortunes' => $fortunes]);
    }

benchmark code: https://github.com/bfinlay/FrameworkBenchmarks/blob/delight.im/frameworks/PHP/delight.im/app/Controller.php

@ocram
Copy link
Contributor

ocram commented May 29, 2019

Excellent point, thank you!

While we only use \PDO::FETCH_ASSOC internally so far, we definitely want to support the following additional “fetch styles”:

\PDO::FETCH_OBJ
    => selectAsObjects + selectRowAsObject
\PDO::FETCH_NUM
    => selectNumbered + selectRowNumbered
\PDO::FETCH_CLASS
    => selectIntoClass + selectRowIntoClass
    or
    => selectAsInstances + selectRowAsInstance
\PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE
    => selectIntoConstructedClass + selectRowIntoConstructedClass
    or
    => selectAsConstructedInstances + selectRowAsConstructedInstance
\PDO::FETCH_KEY_PAIR
    => selectAsPairs + selectRowAsPair
\PDO::FETCH_UNIQUE
    => selectUnique + selectRowUnique
\PDO::FETCH_GROUP
    => selectGrouped + selectRowGrouped

Yes, some of these are a bit verbose. But you have IDEs with autocomplete everywhere, and we currently can’t think of any solution other than these separate methods that we like as much.

What do you think?

@bfinlay
Copy link
Author

bfinlay commented May 29, 2019 via email

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

No branches or pull requests

2 participants