nette/database
Nette Database is a lightweight PHP database layer with a fluent query builder, safe parameter binding, and convenient data fetching. Works with PDO and integrates with Nette, offering transactions, profiling, and easy exploration of results.
composer require nette/database.config.neon or PHP) to register the service:
services:
- Nette\Database\Explorer([
connection => [
driver => 'mysqli',
host => 'localhost',
username => 'root',
password => '',
database => 'myapp'
]
])
or via PHP:
$database = new Nette\Database\Explorer([
'driver' => 'mysqli',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'myapp',
]);
$rows = $database->table('users')->where('active', 1)->fetchAll();
foreach ($rows as $user) {
echo $user->name;
}
$query = $database->table('orders')
->where('status', 'pending')
->limit(10)
->orderBy('created_at', 'DESC');
$pendingOrders = $query->fetchAll();
// Insert
$database->table('users')->insert([
'email' => 'new@example.com',
'created_at' => new DateTime,
]);
// Update
$user = $database->table('users')->get($id);
$user->update(['name' => 'Updated Name']);
join() and select() with alias-friendly syntax.
$result = $database->table('posts')
->select('posts.*, users.name AS author')
->join('users', 'posts.author_id = users.id')
->where('posts.status', 'published')
->fetchAll();
$database->transaction(function (\Nette\Database\Explorer $db) {
$db->table('orders')->insert([...]);
$db->table('inventory')->where('product_id', $id)->decrement('stock');
});
echo $database->getConnection()->debug(); // Current SQL with bound params
nette/database does not auto-join related data like full ORMs. Manually build joins or use fetchPair() + loops for optimization.->table('order').FETCH_ALL vs fetch():
fetchAll() → array of Row objects (or arrays if configured).fetch() → single Row or null.fetchAssoc($key) to group results by a column.where(), andWhere(), etc., but be cautious when using raw SQL fragments (where('id = ?', $id) is safe; where("id = $id") is not).Nette\Database\Table\Selection to add domain-specific scopes (e.g., ActiveSelection).$explorer->getConnection()->close().How can I help you explore Laravel packages today?