PHPackages                             nette/database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. nette/database

ActiveLibrary[Database &amp; ORM](/categories/database)

nette/database
==============

💾 Nette Database: layer with a familiar PDO-like API but much more powerful. Building queries, advanced joins, drivers for MySQL, PostgreSQL, SQLite, MS SQL Server and Oracle.

v3.2.8(6mo ago)5656.7M↓28.5%110[38 issues](https://github.com/nette/database/issues)[20 PRs](https://github.com/nette/database/pulls)20BSD-3-ClausePHPPHP 8.1 - 8.5CI passing

Since May 11Pushed 2mo ago35 watchersCompare

[ Source](https://github.com/nette/database)[ Packagist](https://packagist.org/packages/nette/database)[ Docs](https://nette.org)[ RSS](/packages/nette-database/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (64)Used By (20)

[![Nette Database](https://private-user-images.githubusercontent.com/194960/331215311-97d8f31b-096c-466c-a76f-f5b9e511ea8d.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzQzNDE4ODAsIm5iZiI6MTc3NDM0MTU4MCwicGF0aCI6Ii8xOTQ5NjAvMzMxMjE1MzExLTk3ZDhmMzFiLTA5NmMtNDY2Yy1hNzZmLWY1YjllNTExZWE4ZC5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwMzI0JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDMyNFQwODM5NDBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03YjMwNmMxMTQ5YjQzMTA5YmE4ZWMyMjJlZDM4YWUzZWNkMWRmYzNjOTc2ZTgxZTE2ZjE5N2NjNWNmOTRhMDkxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.9nxA5q5tCwT2SU6IQnCHxeTu0h7uwNNzdua9nCH5OM8)](https://doc.nette.org/database)

[![Downloads this Month](https://camo.githubusercontent.com/b0855f7b929181d1fe401447c027886344ccf9514ae970d9486e935e6c0213a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6e657474652f64617461626173652e737667)](https://packagist.org/packages/nette/database)[![Tests](https://github.com/nette/database/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/nette/database/actions)[![Latest Stable Version](https://camo.githubusercontent.com/6630fe316d170670093d66582bf9caca012d9e9ebf56ae75a67cc8459ec17ffd/68747470733a2f2f706f7365722e707567782e6f72672f6e657474652f64617461626173652f762f737461626c65)](https://github.com/nette/database/releases)[![License](https://camo.githubusercontent.com/fa7d5fcf2c84b580327af52da95dd751703af65f079dc3c5a0081beac0789718/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4e65772532304253442d626c75652e737667)](https://github.com/nette/database/blob/master/license.md)

Introduction
------------

[](#introduction)

Nette provides a powerful layer for accessing your database easily.

✅ composes SQL queries with ease
✅ significantly simplifies retrieving data without writing SQL queries
✅ uses efficient queries and does not transmit unnecessary data

The [Nette Database Core](https://doc.nette.org/en/database/core) is a wrapper around the PDO and provides core functionality.

The [Nette Database Explorer](https://doc.nette.org/en/database/explorer) layer helps you to fetch database data more easily and in a more optimized way.

[Support Me](https://github.com/sponsors/dg)
--------------------------------------------

[](#support-me)

Do you like Nette Database? Are you looking forward to the new features?

[![Buy me a coffee](https://camo.githubusercontent.com/afa7c20ccaac10ac4f1f51669bafb212856b932e0c8b276cb290336cf08624b8/68747470733a2f2f66696c65732e6e657474652e6f72672f69636f6e732f646f6e6174696f6e2d332e737667)](https://github.com/sponsors/dg)

Thank you!

Installation
------------

[](#installation)

The recommended way to install is via Composer:

```
composer require nette/database

```

It requires PHP version 8.2 and supports PHP up to 8.5.

Usage
-----

[](#usage)

This is just a piece of documentation. [Please see our website](https://doc.nette.org/database).

Database Core
-------------

[](#database-core)

To create a new database connection just create a new instance of `Nette\Database\Connection` class:

```
$database = new Nette\Database\Explorer($dsn, $user, $password); // the same arguments as uses PDO
```

Connection allows you to easily query your database by calling `query` method:

```
$database->query('INSERT INTO users', [ // an array can be a parameter
	'name' => 'Jim',
	'created' => new DateTime, // or a DateTime object
	'avatar' => fopen('image.gif', 'r'), // or a file
], ...); // it is even possible to use multiple inserts

$database->query('UPDATE users SET ? WHERE id=?', $data, $id);
$database->query('SELECT * FROM categories WHERE id=?', 123)->dump();
```

Database Explorer
-----------------

[](#database-explorer)

Nette Database Explorer layer helps you to fetch database data more easily and in a more optimized way. The primary attitude is to fetch data only from one table and fetch them at once. The data are fetched into `ActiveRow` instances. Data from other tables connected by relationships are delivered by another queries - this is maintained by Database Explorer layer itself.

Let's take a look at common use-case. You need to fetch books and their authors. It is common 1:N relationship. The often used implementation fetches data by one SQL query with table joins. The second possibility is to fetch data separately, run one query for getting books and then get an author for each book by another query (e.g. in your foreach cycle). This could be easily optimized to run only two queries, one for books, and another for the needed authors - and this is just the way how Nette Database Explorer does it.

Selecting data starts with the table, just call `$explorer->table()` on the `Nette\Database\Explorer` object. The easiest way to get it is [described here](https://doc.nette.org/database-core#toc-configuration), but if we use Nette Database Explorer alone, it can be [manually created](https://doc.nette.org/database-explorer#toc-manual-creating-nette-database-context).

```
$selection = $explorer->table('book'); // db table name is "book"
```

We can simply iterate over the selection and pass through all the books. The rows are fetched as ActiveRow instances; you can read row data from their properties.

```
$books = $explorer->table('book');
foreach ($books as $book) {
	echo $book->title;
	echo $book->author_id;
}
```

Getting just one specific row is done by `get()` method, which directly returns an ActiveRow instance.

```
$book = $explorer->table('book')->get(2); // returns book with id 2
echo $book->title;
echo $book->author_id;
```

Working with relationships
--------------------------

[](#working-with-relationships)

```
$books = $explorer->table('book');

foreach ($books as $book) {
	echo 'title:      ' . $book->title;
	echo 'written by: ' . $book->author->name;

	echo 'tags: ';
	foreach ($book->related('book_tag') as $bookTag) {
		echo $bookTag->tag->name . ', ';
	}
}
```

You will be pleased how efficiently the database layer works. The example above performs constant number of queries, see following 4 queries:

```
SELECT * FROM `book`
SELECT * FROM `author` WHERE (`author`.`id` IN (11, 12))
SELECT * FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
SELECT * FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))
```

If you use caching (defaults on), no columns will be queried unnecessarily. After the first query, cache will store the used column names and Nette Database Explorer will run queries only with the needed columns:

```
SELECT `id`, `title`, `author_id` FROM `book`
SELECT `id`, `name` FROM `author` WHERE (`author`.`id` IN (11, 12))
SELECT `book_id`, `tag_id` FROM `book_tag` WHERE (`book_tag`.`book_id` IN (1, 4, 2, 3))
SELECT `id`, `name` FROM `tag` WHERE (`tag`.`id` IN (21, 22, 23))
```

[Continue…](https://doc.nette.org/database-explorer).

###  Health Score

75

—

ExcellentBetter than 100% of packages

Maintenance76

Regular maintenance activity

Popularity66

Solid adoption and visibility

Community52

Growing community involvement

Maturity95

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 65.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~106 days

Total

62

Last Release

71d ago

Major Versions

v2.4.9 → v3.0.52019-12-13

v2.4.10 → v3.0.72020-10-30

v2.4.11 → v3.1.02020-12-21

v2.4.12 → v3.1.62022-11-18

v3.2.4 → v4.0.0-RC12024-08-29

PHP version history (11 changes)v2.2.0PHP &gt;=5.3.1

v2.4.0PHP &gt;=5.6.0

v3.0.0PHP &gt;=7.1

v2.4.11PHP &gt;=5.6 &lt;8.1

v3.1.0PHP &gt;=7.2 &lt;8.1

v3.1.4PHP &gt;=7.2 &lt;8.2

v3.1.6PHP &gt;=7.2 &lt;8.3

v3.1.9PHP 7.2 - 8.3

v3.2.0PHP 8.1 - 8.3

v3.2.4PHP 8.1 - 8.4

v3.2.8PHP 8.1 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/17f266513a3ca97500ec3d85d562b9279c7a6346358fe2b8d90390ece717a027?d=identicon)[david@grudl.com](/maintainers/david@grudl.com)

---

Top Contributors

[![dg](https://avatars.githubusercontent.com/u/194960?v=4)](https://github.com/dg "dg (762 commits)")[![hrach](https://avatars.githubusercontent.com/u/284263?v=4)](https://github.com/hrach "hrach (259 commits)")[![milo](https://avatars.githubusercontent.com/u/439140?v=4)](https://github.com/milo "milo (34 commits)")[![norbe](https://avatars.githubusercontent.com/u/194486?v=4)](https://github.com/norbe "norbe (16 commits)")[![foxycode](https://avatars.githubusercontent.com/u/1284781?v=4)](https://github.com/foxycode "foxycode (9 commits)")[![Unlink](https://avatars.githubusercontent.com/u/9026197?v=4)](https://github.com/Unlink "Unlink (9 commits)")[![vrana](https://avatars.githubusercontent.com/u/117453?v=4)](https://github.com/vrana "vrana (7 commits)")[![adaamz](https://avatars.githubusercontent.com/u/4347332?v=4)](https://github.com/adaamz "adaamz (6 commits)")[![enumag](https://avatars.githubusercontent.com/u/539462?v=4)](https://github.com/enumag "enumag (6 commits)")[![EdaCZ](https://avatars.githubusercontent.com/u/1671637?v=4)](https://github.com/EdaCZ "EdaCZ (5 commits)")[![fabik](https://avatars.githubusercontent.com/u/816866?v=4)](https://github.com/fabik "fabik (4 commits)")[![insekticid](https://avatars.githubusercontent.com/u/177340?v=4)](https://github.com/insekticid "insekticid (4 commits)")[![mishak87](https://avatars.githubusercontent.com/u/276500?v=4)](https://github.com/mishak87 "mishak87 (3 commits)")[![JanTvrdik](https://avatars.githubusercontent.com/u/175109?v=4)](https://github.com/JanTvrdik "JanTvrdik (3 commits)")[![h4kuna](https://avatars.githubusercontent.com/u/335722?v=4)](https://github.com/h4kuna "h4kuna (3 commits)")[![juzna](https://avatars.githubusercontent.com/u/227416?v=4)](https://github.com/juzna "juzna (3 commits)")[![paranoiq](https://avatars.githubusercontent.com/u/146912?v=4)](https://github.com/paranoiq "paranoiq (3 commits)")[![fprochazka](https://avatars.githubusercontent.com/u/158625?v=4)](https://github.com/fprochazka "fprochazka (2 commits)")[![JosefDohnal](https://avatars.githubusercontent.com/u/6528948?v=4)](https://github.com/JosefDohnal "JosefDohnal (2 commits)")[![Majkl578](https://avatars.githubusercontent.com/u/144181?v=4)](https://github.com/Majkl578 "Majkl578 (2 commits)")

---

Tags

database-layernettenette-frameworkormpdophpsql-querynettedatabasemysqlsqlitepostgresqlpdomssqloraclequeriesnotorm

### Embed Badge

![Health badge](/badges/nette-database/health.svg)

```
[![Health](https://phpackages.com/badges/nette-database/health.svg)](https://phpackages.com/packages/nette-database)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[dibi/dibi

Dibi is Database Abstraction Library for PHP

5013.8M120](/packages/dibi-dibi)[catfan/medoo

The lightweight PHP database framework to accelerate development

4.9k1.5M194](/packages/catfan-medoo)[dg/adminer-custom

Customization for Adminer, the best database management tool written in PHP.

134765.7k16](/packages/dg-adminer-custom)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)[ramadan/easy-model

A Laravel package for enjoyably managing database queries.

101.6k](/packages/ramadan-easy-model)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
