PHPackages                             nanodb/nanodb - 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. nanodb/nanodb

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

nanodb/nanodb
=============

ORM

2.6.9(4y ago)0327PHPPHP &gt;=7.2.0

Since Apr 16Pushed 4y ago1 watchersCompare

[ Source](https://github.com/yar3333/nanodb)[ Packagist](https://packagist.org/packages/nanodb/nanodb)[ RSS](/packages/nanodb-nanodb/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (113)Used By (0)

nanodb
======

[](#nanodb)

Small, fast and free database-first ORM for PHP 7.2+ and MySQL

Install
-------

[](#install)

```
composer require nanodb/nanodb
```

Generate PHP classes (models &amp; managers) from database
----------------------------------------------------------

[](#generate-php-classes-models--managers-from-database)

```
# windows
php vendor\nanodb\nanodb\cli.php  [options]

#linux
php vendor/nanodb/nanodb/cli.php  [options]
```

`` must be in URI format: `mysql://user:password@host:port/database`

Options:

```
-a, --autogenerated-namespace  Namespace for autogenerated classes.
                               Default is 'models\autogenerated'.

-c, --custom-namespace         Namespace for your custom classes.
                               Default is 'models'.

-o, --out-path                 This is a base directory path for generated files.

-i, --ignore-table             Table name to ignore.

-nim, --no-instantiate-manager Table name to skip manager creating in autogenerated Orm class.
                               You can use this switch for your managers with a custom constructors.
                               In this case you must instantiate these managers manually
                               (in regular case - in your custom Orm constructor).

-pf, --position-field          Field name treated as record number (1, 2, 3, ...).
                               Values of such fields will be autocalculated on records creating.
                               Can be specified in next forms:
                                `field` or `*.field` - to specify fields in any table;
                                `table.field` - to specify field in specified table only.
                               Default is `position`.

```

Generator will look to:

- primary keys
- autoincrements
- foreign keys
- indexes

Using code example
------------------

[](#using-code-example)

Assumed you have a `users` table in your database with fields:

- `id` int autoincrement
- `login` varchar unique index
- `role` varchar index
- `status` int

```
use \nanodb\orm\Db;
use \nanodb\orm\SqlText;
use \models\Orm;

$db = new Db("mysql://root:123456@localhost/testdb");
$orm = new Orm($db);

#################
# High-level code
#################

# create user and insert into database
$user = new User();
$user->login = $login;
$user->role = $role;
$user->status = $status;
$orm->user->add($user);

# get user by ID
$user = $orm->user->get(10);

# field name and value will be automatically quoted
$users = $orm->user->whereField("status", "=", 2)->findMany();

# field name and value will be automatically quoted
$users = $orm->user->whereField("status", "IN", [2, 3])->findMany();

# prevent quoting by SqlText::raw()
$users = $orm->user->whereField("status", "=", SqlText::raw("1 + 1"))->findMany();

# find by raw SQL condition
$users = $orm->user->where("status = 2")->findMany();

# find first by raw SQL
$user = $orm->user->getOne("SELECT * FROM `users` WHERE `status` = " . $db->quote($statusFromRequest));

# find many by raw SQL
$users = $orm->user->getMany("SELECT * FROM `users` WHERE `status` = 2");

# find by raw SQL with binding parameters
$users = $orm->user->getMany("SELECT * FROM `users` WHERE `status` = {myStatus}", [
	"myStatus" => $statusFromRequest
]);

# get count of all users
$count = $orm->user->count();

# get count of users by complex condition
$count = $orm->user->whereField("status", "=", 1)
                   ->whereField("role", "=", "support")
                   ->count();

# removing
$orm->user->deleteById(10);
$orm->user->whereField("status", "!=", 2)->delete();

# if you have unique index by `login` field, then you can do next
$user = $orm->user->getByLogin('root');

# if you have regular index by `role` field, then you can do next
$users = $orm->user->getByRole('support');

# if you have a `books` table with `user_id` field (with a foreign key), then you can do next
$books = $orm->book->getByUserId(10);

# get a user and fix `status`
$user = $orm->user->get(10);
$user->status = 5;
$orm->user->save($user);

################
# Low-level code
################

$rows = $db->query("SELECT `role`, `status` FROM `users`")->results();
$count = $db->query("SELECT COUNT(*) FROM `users`")->getIntResult(0);

$resultSet = $db->query("SELECT `role`, `status` FROM `users`");
while ($row = $resultSet->next())
{
	echo "role = " . $row['role'] . "; status = " . $row['status'] . "\n";
}
```

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 95.6% 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 ~10 days

Recently: every ~122 days

Total

112

Last Release

1473d ago

Major Versions

1.8.6 → 2.0.02019-06-15

PHP version history (2 changes)1.0.0PHP &gt;=7.1.0

1.1.0PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/62287c6031fadbee2c2b9c588d6f8f843e511bafec08e9cdbe12e0fae5a05de3?d=identicon)[yar3333](/maintainers/yar3333)

---

Top Contributors

[![yar3333](https://avatars.githubusercontent.com/u/900330?v=4)](https://github.com/yar3333 "yar3333 (152 commits)")[![Isludnikov](https://avatars.githubusercontent.com/u/29144634?v=4)](https://github.com/Isludnikov "Isludnikov (7 commits)")

### Embed Badge

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

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

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k116.5M113](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)

PHPackages © 2026

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