Skip to content

Commit

Permalink
[5.2] Properly support PDO::FETCH_CLASS in cursor() (#14052)
Browse files Browse the repository at this point in the history
* Use variable

* Properly support PDO::FETCH_CLASS in cursor()

* Refine handling of PDO::FETCH_CLASS without class name

* Adjustment in testAlternateFetchModes()
  • Loading branch information
vlakoff authored and taylorotwell committed Jun 27, 2016
1 parent 8989470 commit 9a925e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 18 additions & 6 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,16 @@ public function select($query, $bindings = [], $useReadPdo = true)

$statement->execute($me->prepareBindings($bindings));

$fetchMode = $me->getFetchMode();
$fetchArgument = $me->getFetchArgument();

return isset($fetchArgument) ?
$statement->fetchAll($me->getFetchMode(), $fetchArgument, $me->getFetchConstructorArgument()) :
$statement->fetchAll($me->getFetchMode());
if ($fetchMode === PDO::FETCH_CLASS && ! isset($fetchArgument)) {
$fetchArgument = 'StdClass';
}

return isset($fetchArgument)
? $statement->fetchAll($fetchMode, $fetchArgument, $me->getFetchConstructorArgument())
: $statement->fetchAll($fetchMode);
});
}

Expand All @@ -359,10 +364,17 @@ public function cursor($query, $bindings = [], $useReadPdo = true)

$statement = $this->getPdoForSelect($useReadPdo)->prepare($query);

if ($me->getFetchMode() === PDO::FETCH_CLASS) {
$statement->setFetchMode($me->getFetchMode(), 'StdClass');
$fetchMode = $me->getFetchMode();
$fetchArgument = $me->getFetchArgument();

if ($fetchMode === PDO::FETCH_CLASS && ! isset($fetchArgument)) {
$fetchArgument = 'StdClass';
}

if (isset($fetchArgument)) {
$statement->setFetchMode($fetchMode, $fetchArgument, $me->getFetchConstructorArgument());
} else {
$statement->setFetchMode($me->getFetchMode());
$statement->setFetchMode($fetchMode);
}

$statement->execute($me->prepareBindings($bindings));
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function testAlternateFetchModes()
$stmt->expects($this->exactly(3))->method('fetchAll')->withConsecutive(
[PDO::FETCH_ASSOC],
[PDO::FETCH_COLUMN, 3, []],
[PDO::FETCH_CLASS, 'stdClass', [1, 2, 3]]
[PDO::FETCH_CLASS, 'ClassName', [1, 2, 3]]
);
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->any())->method('prepare')->will($this->returnValue($stmt));
Expand All @@ -309,7 +309,7 @@ public function testAlternateFetchModes()
$connection->select('SELECT * FROM foo');
$connection->setFetchMode(PDO::FETCH_COLUMN, 3);
$connection->select('SELECT * FROM foo');
$connection->setFetchMode(PDO::FETCH_CLASS, 'stdClass', [1, 2, 3]);
$connection->setFetchMode(PDO::FETCH_CLASS, 'ClassName', [1, 2, 3]);
$connection->select('SELECT * FROM foo');
}

Expand Down

0 comments on commit 9a925e4

Please sign in to comment.