nanodb / nanodb
There is no license information available for the latest version (2.6.9) of this package.
ORM
2.6.9
2022-06-15 09:18 UTC
Requires
- php: >=7.2.0
- dev-master
- 2.6.9
- 2.6.8
- 2.6.7
- 2.6.6
- 2.6.5
- 2.6.4
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.0
- 2.4.0
- 2.3.0
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.8.6
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.7
- 1.7.6
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.0
- 1.5.9
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2025-03-15 15:29:35 UTC
README
Small, fast and free database-first ORM for PHP 7.2+ and MySQL
Install
composer require nanodb/nanodb
Generate PHP classes (models & managers) from database
# windows php vendor\nanodb\nanodb\cli.php <databaseConnectionString> [options] #linux php vendor/nanodb/nanodb/cli.php <databaseConnectionString> [options]
<databaseConnectionString>
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
Assumed you have a users
table in your database with fields:
id
int autoincrementlogin
varchar unique indexrole
varchar indexstatus
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"; }