Skip to content

Commit 3e3d96d

Browse files
author
Gusakov Nikita
committed
Added fetch mode configuration
1 parent a3d3801 commit 3e3d96d

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/Connection.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class Connection extends \PDO
5151
public function __construct($dsn, $username = null, $password = null, array $attributes = [], Grammar $grammar = null)
5252
{
5353
parent::__construct($dsn, $username, $password, array_replace($attributes, [
54-
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
55-
\PDO::ATTR_STATEMENT_CLASS => ['Flame\\Query', [&$this->grammar, &$this->placeholders, &$this->types]]
54+
self::ATTR_ERRMODE => self::ERRMODE_EXCEPTION,
55+
self::ATTR_DEFAULT_FETCH_MODE => self::FETCH_ASSOC,
56+
self::ATTR_STATEMENT_CLASS => ['Flame\\Query', [&$this->grammar, &$this->placeholders, &$this->types]],
5657
]));
5758

5859
if ($grammar === null) {
@@ -113,6 +114,18 @@ public function commit()
113114
return $this;
114115
}
115116

117+
/**
118+
* @param int $mode
119+
*
120+
* @return static
121+
*/
122+
public function setDefaultFetchMode($mode)
123+
{
124+
$this->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, $mode);
125+
126+
return $this;
127+
}
128+
116129
/**
117130
* @param string $column,...
118131
*

src/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function closeCursor()
8181
*
8282
* @return array
8383
*/
84-
public function fetchCallback(callable $callback, $mode = \PDO::FETCH_ASSOC)
84+
public function fetchCallback(callable $callback, $mode = null)
8585
{
8686
$results = [];
8787
while (false !== $row = $this->fetch($mode)) {

tests/ConnectionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ protected function setUp()
1818
$this->connection->query('CREATE TABLE users (id INT PRIMARY KEY, username CHAR(50), sex BOOL)');
1919
}
2020

21+
public function testSetDefaultFetchMode()
22+
{
23+
$this->assertSame(\PDO::FETCH_ASSOC, $this->connection->getAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE));
24+
25+
$mode = \PDO::FETCH_OBJ;
26+
27+
$this->assertSame($this->connection, $this->connection->setDefaultFetchMode($mode));
28+
$this->assertSame($mode, $this->connection->getAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE));
29+
}
30+
2131
public function testAnotherGrammar()
2232
{
2333
$grammar = new MysqlGrammar();

0 commit comments

Comments
 (0)