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

Add a fetch mode methods for "PDO::FETCH_KEY_PAIR" #4289

Closed
tswcode opened this issue Sep 23, 2020 · 6 comments · Fixed by #4293 or #4338
Closed

Add a fetch mode methods for "PDO::FETCH_KEY_PAIR" #4289

tswcode opened this issue Sep 23, 2020 · 6 comments · Fixed by #4293 or #4338

Comments

@tswcode
Copy link

tswcode commented Sep 23, 2020

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

Let's assume a sql, summarizing entity ids and a special count. Then I need the workflow like the default PDO does:

public function countOrders()
{
    $sql = 'SELECT `e`.`id`, COUNT(`o`.`id`) 
        FROM `event` AS `e` 
        LEFT JOIN `order` AS `o` ON `o`.`event_id` = `e`.`id`
        WHERE `o`.`deleted` = 0
        GROUP BY `e`.`id` ';

    $dbConnection = $this->getEntityManager()->getConnection();
    $sqlStatement = $dbConnection->prepare($sql);
    $sqlStatement->execute();

    $counts = $sqlStatement->fetchAll(PDO::FETCH_KEY_PAIR); 
    $sqlStatement->free();

    return $counts;
}

The result was an array with the ids as keys and counts as values, like:

array (2) [
    11 => integer 10
    22 => integer 5
]

Since the old fetch methods are deprecated, it would be great to implement
an adequate method to the PDO feature to reduce boilerplate code.

Here is an quote from @morozov of #4019

$conn = new PDO('sqlite::memory:');
$stmt = $conn->query('SELECT 1');

$stmt->fetchAll(PDO::FETCH_KEY_PAIR);

// PDOStatement::fetchAll(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 columns

As @morozov mentioned:

Looks like PDO is strict enough about the number of columns. I think we can implement this in DBAL at the wrapper level which will automatically cover all supported drivers.

@morozov
Copy link
Member

morozov commented Sep 23, 2020

$sqlStatement->fetchAll(PDO::FETCH_KEY_PAIR, 0);

@tswcode what does the second argument in this call mean? I cannot find any documentation for it.

@tswcode
Copy link
Author

tswcode commented Sep 23, 2020

Oh sorry, that was copy and paste. I guess it previously was an PDO::FETCH_COLUMN. I'll edit the first post.

@Gamesh
Copy link

Gamesh commented Oct 9, 2020

here is a good documentation of this and all the other modes that are now not accessible from DBAL https://phpdelusions.net/pdo/fetch_modes

@morozov morozov added this to the 2.11.2 milestone Oct 14, 2020
@morozov morozov linked a pull request Oct 14, 2020 that will close this issue
@morozov
Copy link
Member

morozov commented Oct 14, 2020

Closed via #4338.

@morozov morozov self-assigned this Oct 14, 2020
rgrellmann added a commit to Rossmann-IT/dbal that referenced this issue Mar 7, 2021
Release [2.11.2](https://github.com/doctrine/dbal/milestone/81)

2.11.2
======

- Total issues resolved: **5**
- Total pull requests resolved: **16**
- Total contributors: **10**

Static Analysis
---------------

 - [4353: Update Psalm to 3.17.2 and lock the version used with GitHub Actions](doctrine#4353) thanks to @morozov
 - [4348: Bump Psalm level to 3](doctrine#4348) thanks to @morozov
 - [4332: Static analysis improvements](doctrine#4332) thanks to @morozov
 - [4319: Bump Psalm level to 4](doctrine#4319) thanks to @morozov

Code Style
----------

 - [4346: Minor CS improvement - use ::class for TestCase::expectException input](doctrine#4346) thanks to @mvorisek

 - [4344: Static analysis workflow](doctrine#4344) thanks to @greg0ire
 - [4340: Modernize existing ga](doctrine#4340) thanks to @greg0ire
 - [4309: Use cache action v2](doctrine#4309) thanks to @greg0ire
 - [4305: Move website config to default branch](doctrine#4305) thanks to @SenseException

Improvement,Prepared Statements
-------------------------------

 - [4341: Add Statement::fetchAllIndexedAssociative() and ::iterateIndexedAssociative()](doctrine#4341) thanks to @morozov and @ZaneCEO
 - [4338: Add Statement::fetchAllKeyValue() and ::iterateKeyValue()](doctrine#4338) thanks to @morozov

BC Fix,Query
------------

 - [4330: Fix regression in QueryBuilder::and|orWhere()](doctrine#4330) thanks to @BenMorel

Test Suite
----------

 - [4321: Update PHPUnit to 9.4](doctrine#4321) thanks to @morozov

Columns,SQL Server,Schema Managers
----------------------------------

 - [4315: Fix handling existing SQL Server column comment when other properties change](doctrine#4315) thanks to @trusek

CI
--

 - [4310: Migrate jobs away from Travis to Github Actions ](doctrine#4310) thanks to @greg0ire

BC Fix,Connections
------------------

 - [4308: doctrine#4295 Keep master, slaves, keepReplica params in MasterSlaveConnection](doctrine#4308) thanks to @kralos

New Feature,Prepared Statements
-------------------------------

 - [4289: Add a fetch mode methods for "PDO::FETCH&doctrine#95;KEY&doctrine#95;PAIR"](doctrine#4289) thanks to @tswcode

Bug,SQL Server,Schema
---------------------

 - [3400: Wrong column comment setting command in migrations of SQL Server](doctrine#3400) thanks to @msyfurukawa

# gpg: Signature made Mon Oct 19 04:18:17 2020
# gpg:                using DSA key 1BEDEE0A820BC30D858F9F0C2C3A645671828132
# gpg: Can't check signature: No public key
@uncaught
Copy link

uncaught commented Jul 10, 2021

@morozov I am a bit confused about the interfaces returned by executeQuery() and I am not getting code completion for fetchAllKeyValue(). Is the method missing somewhere? I am not seeing it in Doctrine\DBAL\Driver\PDOStatement nor in Doctrine\DBAL\Driver\Result where I find the other mapping fetches like fetchAllAssociative().

(I'm still on 2.12.x, but not seeing it in the 2.13 branch, either)

Edit: Sorry, never mind, didn't realize they are all on the connection now directly!

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.