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
2 changes: 1 addition & 1 deletion src/Bridges/DatabaseDI/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
namespace Nette\Bridges\DatabaseDI;

use Nette;
use Nette\Database\Table\ActiveRow;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
use Tracy;
use function is_array, is_string;
use Nette\Database\Table\ActiveRow;


/**
Expand Down
12 changes: 12 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,18 @@ public function fetchAll(#[Language('SQL')] string $sql, #[Language('GenericSQL'
}


/**
* Executes SQL query and returns all rows as an array of arrays.
* Useful because the rows can be typed using @var.
* @param literal-string $sql
* @return list<array>
*/
public function fetchAllAssoc(#[Language('SQL')] string $sql, #[Language('GenericSQL')] mixed ...$params): array
{
return $this->query($sql, ...$params)->fetchAllAssoc();
}


/**
* Creates SQL literal value.
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Database/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Result implements \IteratorAggregate
/** @var list<Row> */
private array $rows;

/** @var list<array> */
private array $rowsArray;

/** @var array<string, array{name: string, nativeType: string, table?: string}> */
private array $meta;

Expand Down Expand Up @@ -204,6 +207,25 @@ public function fetchAll(): array
}


/**
* Returns all remaining rows as array of associative arrays.
* Useful because the rows can be typed using @var.
* @return list<array>
*/
public function fetchAllAssoc(): array
{
if(!isset($this->rowsArray)){
$this->rowsArray = [];
while(true){
$data = $this->fetchAssoc();
if($data === null) break;
$this->rowsArray[] = $data;
}
}
return $this->rowsArray;
}


private function normalizeRow(array $row): array
{
$engine = $this->explorer->getDatabaseEngine();
Expand Down
1 change: 0 additions & 1 deletion src/Database/Table/GroupedSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Nette\Database\Table;

use Nette;
use Nette\Database\Explorer;
use function array_keys, count, iterator_to_array, preg_match, reset;

Expand Down
6 changes: 6 additions & 0 deletions tests/types/database-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ function testResultSetFetchAll(ResultSet $resultSet): void
}


function testResultSetFetchAllAssoc(ResultSet $resultSet): void
{
assertType('list<array>', $resultSet->fetchAllAssoc());
}


function testResultSetFetchPairs(ResultSet $resultSet): void
{
assertType('array<mixed>', $resultSet->fetchPairs());
Expand Down