Core\Db\Rows::all
$rows->all(): array<T>Reads every row of a query at once, as an array.
Core\Db\Rows::all returns one entry per row of the result. Each entry is a Core\Db\Row, and the entries come in the order the server sent them. A query that matched no row returns an empty array.
The rows were already read from the database when query returned. This builds one small object per row and reads nothing again, so you can call it as often as you like, and every call gives you the same rows.
Where you wrote Core\Db\Connection::queryAs with a class of your own, each entry is an object of that class.
The examples below read every row at once, read the rows of one query more than once, and hand a whole result set to another method.
Return value
Returns array<T>. An array<T>. It is empty for a statement that returned no rows. T is a Core\Db\Row for query, and the class you named for queryAs<T>. The rows are already read, so this creates one object for each row and decodes nothing again.
Examples
The examples below run with this configuration file.
# The `notes` database is kept in memory. It is deleted when the program ends.[db.notes]driver = "sqlite"path = ":memory:"
# The program may open the `notes` database.[capabilities.db]connect = ["notes"]Read every row at once
<?nvs// Shows how to read every row of a query in one step.
var $db = Core\Db::connect('notes');$db->execute('create table guests (id integer primary key, name text not null, seats int not null)', []);$db->execute('insert into guests (name, seats) values (?, ?), (?, ?), (?, ?)', ['Ada Ahmed', 4, 'Bo Berg', 2, 'Cem Celik', 6]);
// `all` returns every row of the result as an array. Each entry is one// `Core\Db\Row`, and the entries come in the order the query asked for.array<Core\Db\Row> $rows = $db->query('select name, seats from guests order by id', [])->all();
// This prints one line per guest, starting with `Ada Ahmed booked 4 seats`.foreach ($rows as Core\Db\Row $row) { echo $row->string('name') ?? '', ' booked ', $row->int('seats') ?? 0, " seats\n";}
// The array is a normal array, so you can count it. This prints `3`.echo Core\Arr::count($rows), "\n";
$db->close();Ada Ahmed booked 4 seatsBo Berg booked 2 seatsCem Celik booked 6 seats3Read the rows more than once
<?nvs// Shows how to read the rows of one query more than once.
var $db = Core\Db::connect('notes');$db->execute('create table bookings (id integer primary key, guest text not null, seats int not null)', []);$db->execute('insert into bookings (guest, seats) values (?, ?), (?, ?)', ['Ada Ahmed', 4, 'Bo Berg', 2]);
array<Core\Db\Row> $rows = $db->query('select guest, seats from bookings order by id', [])->all();
// The first pass adds the seats together. This prints `seats: 6`.int $seats = 0;foreach ($rows as Core\Db\Row $row) { $seats = $seats + ($row->int('seats') ?? 0);}echo 'seats: ', $seats, "\n";
// The second pass reads the same rows again. The array holds the rows, so this// reads nothing from the database.foreach ($rows as Core\Db\Row $row) { echo $row->string('guest') ?? '', "\n";}
// A query that matches no row returns an empty array. This prints `0`.array<Core\Db\Row> $none = $db->query('select guest, seats from bookings where seats > ?', [100])->all();echo Core\Arr::count($none), "\n";
$db->close();seats: 6Ada AhmedBo Berg0Hand a whole result set to another method
<?nvs// Shows how to give a whole result set to another part of your program.use Core\Db\Derive;
// `#[Derive]` writes the code that fills this class from a row. Each property// takes the column of the same name.#[Derive]class Order{ public uint $id; public int $cents;
public function constructor(uint $id, int $cents) { $this->id = $id; $this->cents = $cents; }}
class Invoice{ // The parameter type says what this method needs: an array of `Order`. The // statement stays in the method that reads it, and this one only counts. public static function total(array<Order> $orders): int { int $cents = 0; foreach ($orders as Order $order) { $cents = $cents + $order->cents; } return $cents; }}
var $db = Core\Db::connect('notes');$db->execute('create table orders (id integer primary key, cents int not null)', []);$db->execute('insert into orders (cents) values (?), (?), (?)', [1200, 4500, 800]);
// `queryAs<Order>` names the class, so `all` returns an `array<Order>`. The// array goes to the method above, which never sees the database.array<Order> $orders = $db->queryAs<Order>('select id, cents from orders order by id', [])->all();
// This prints `orders: 3` and `total: 6500`.echo 'orders: ', Core\Arr::count($orders), "\n";echo 'total: ', Invoice::total($orders), "\n";
$db->close();orders: 3total: 6500