Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/FakePdoStatementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,30 @@ function ($row) use ($fetch_argument, $ctor_args) {
);
}

if ($fetch_style === \PDO::FETCH_KEY_PAIR) {
if (!$this->result) {
return [];
}

$output = [];

foreach ($this->result as $row) {
if ($this->conn->shouldStringifyResult()) {
$row = self::stringify($row);
}

$values = \array_values($row);

if (\count($values) < 2) {
throw new \PDOException('PDO::FETCH_KEY_PAIR requires at least two columns');
}

$output[$values[0]] = $values[1];
}

return $output;
}

throw new \Exception('Fetch style not implemented');
}

Expand Down
17 changes: 17 additions & 0 deletions tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public function testSelectFetchAssoc()
);
}

public function testSelectFetchKeyPair()
{
$pdo = self::getConnectionToFullDB();

$query = $pdo->prepare("SELECT id, name FROM `video_game_characters` WHERE `id` > :id ORDER BY `id` ASC");
$query->bindValue(':id', 14);
$query->execute();

$this->assertSame(
[
'15' => 'link',
'16' => 'dude'
],
$query->fetchAll(\PDO::FETCH_KEY_PAIR)
);
}

public function testSelectFetchAssocConverted()
{
$pdo = self::getConnectionToFullDB(false);
Expand Down
Loading