Skip to content

Commit 4ad40db

Browse files
committed
feat: Support PDO::FETCH_KEY_PAIR
1 parent 839b2fb commit 4ad40db

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/FakePdoStatementTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,30 @@ function ($row) use ($fetch_argument, $ctor_args) {
508508
);
509509
}
510510

511+
if ($fetch_style === \PDO::FETCH_KEY_PAIR) {
512+
if (!$this->result) {
513+
return [];
514+
}
515+
516+
$output = [];
517+
518+
foreach ($this->result as $row) {
519+
if ($this->conn->shouldStringifyResult()) {
520+
$row = self::stringify($row);
521+
}
522+
523+
$values = \array_values($row);
524+
525+
if (\count($values) < 2) {
526+
throw new \PDOException('PDO::FETCH_KEY_PAIR requires at least two columns');
527+
}
528+
529+
$output[$values[0]] = $values[1];
530+
}
531+
532+
return $output;
533+
}
534+
511535
throw new \Exception('Fetch style not implemented');
512536
}
513537

tests/EndToEndTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ public function testSelectFetchAssoc()
9595
);
9696
}
9797

98+
public function testSelectFetchKeyPair()
99+
{
100+
$pdo = self::getConnectionToFullDB();
101+
102+
$query = $pdo->prepare("SELECT id, name FROM `video_game_characters` WHERE `id` > :id ORDER BY `id` ASC");
103+
$query->bindValue(':id', 14);
104+
$query->execute();
105+
106+
$this->assertSame(
107+
[
108+
'15' => 'link',
109+
'16' => 'dude'
110+
],
111+
$query->fetchAll(\PDO::FETCH_KEY_PAIR)
112+
);
113+
}
114+
98115
public function testSelectFetchAssocConverted()
99116
{
100117
$pdo = self::getConnectionToFullDB(false);

0 commit comments

Comments
 (0)