diff --git a/lib/Doctrine/DBAL/Statement.php b/lib/Doctrine/DBAL/Statement.php index 0e47e09303c..294866921ec 100644 --- a/lib/Doctrine/DBAL/Statement.php +++ b/lib/Doctrine/DBAL/Statement.php @@ -264,7 +264,15 @@ public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEX */ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) { - return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs); + if ($ctorArgs !== null) { + return $this->stmt->fetchAll($fetchMode, $fetchArgument, $ctorArgs); + } + + if ($fetchArgument !== null) { + return $this->stmt->fetchAll($fetchMode, $fetchArgument); + } + + return $this->stmt->fetchAll($fetchMode); } /** diff --git a/tests/Doctrine/Tests/DBAL/Functional/ExternalPDOInstanceTest.php b/tests/Doctrine/Tests/DBAL/Functional/ExternalPDOInstanceTest.php new file mode 100644 index 00000000000..42af9d29ea5 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/ExternalPDOInstanceTest.php @@ -0,0 +1,70 @@ +getDriver() instanceof PDOSqliteDriver) { + $this->markTestSkipped('External PDO instance tests are only run on PDO SQLite for now'); + } + + $pdo = new PDO('sqlite::memory:'); + + $this->connection = new Connection(['pdo' => $pdo], new PDOSqliteDriver()); + + $table = new Table('stmt_fetch_all'); + $table->addColumn('a', 'integer'); + $table->addColumn('b', 'integer'); + + $this->connection->getSchemaManager()->createTable($table); + + $this->connection->insert('stmt_fetch_all', [ + 'a' => 1, + 'b' => 2, + ]); + } + + public function testFetchAllWithOneArgument(): void + { + $stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all'); + $stmt->execute(); + + self::assertEquals([[1, 2]], $stmt->fetchAll(FetchMode::NUMERIC)); + } + + public function testFetchAllWithTwoArguments(): void + { + $stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all'); + $stmt->execute(); + + self::assertEquals([2], $stmt->fetchAll(FetchMode::COLUMN, 1)); + } + + public function testFetchAllWithThreeArguments(): void + { + $stmt = $this->connection->prepare('SELECT a, b FROM stmt_fetch_all'); + $stmt->execute(); + + [$obj] = $stmt->fetchAll(FetchMode::CUSTOM_OBJECT, StatementTestModel::class, ['foo', 'bar']); + + $this->assertInstanceOf(StatementTestModel::class, $obj); + + self::assertEquals(1, $obj->a); + self::assertEquals(2, $obj->b); + self::assertEquals('foo', $obj->x); + self::assertEquals('bar', $obj->y); + } +} diff --git a/tests/Doctrine/Tests/DBAL/Functional/StatementTestModel.php b/tests/Doctrine/Tests/DBAL/Functional/StatementTestModel.php new file mode 100644 index 00000000000..16bb6e1e1a3 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/StatementTestModel.php @@ -0,0 +1,24 @@ +x = $x; + $this->y = $y; + } + + /** @var int */ + public $a; + + /** @var int */ + public $b; + + /** @var string */ + public $x; + + /** @var string */ + public $y; +}