PHPackages                             mathsgod/r-db - 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. mathsgod/r-db

ActiveLibrary

mathsgod/r-db
=============

A lightweight orm library for mysql

5.10.0(1y ago)0570↓100%18MITPHPPHP &gt;=8.0CI failing

Since Mar 12Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/mathsgod/r-db)[ Packagist](https://packagist.org/packages/mathsgod/r-db)[ RSS](/packages/mathsgod-r-db/feed)WikiDiscussions next Synced 1mo ago

READMEChangelog (10)Dependencies (14)Versions (155)Used By (8)

[![PHP Composer](https://github.com/mathsgod/r-db/actions/workflows/php.yml/badge.svg)](https://github.com/mathsgod/r-db/actions/workflows/php.yml)

[![Ask DeepWiki](https://camo.githubusercontent.com/0f5ae213ac378635adeb5d7f13cef055ad2f7d9a47b36de7b1c67dbe09f609ca/68747470733a2f2f6465657077696b692e636f6d2f62616467652e737667)](https://deepwiki.com/mathsgod/r-db)

r-db
====

[](#r-db)

Install
-------

[](#install)

```
composer require mathsgod/r-db

```

Setup
-----

[](#setup)

### using .env

[](#using-env)

Using .env file to setup default database connection

```
DATABASE_HOSTNAME=
DATABASE_DATABASE=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_PORT=
DATABASE_CHARSET=
```

Function Q
----------

[](#function-q)

Function Q is a fast way to select data from database.

### simple select

[](#simple-select)

This will select all data from table User and output as array of stdClass

```
use function R\DB\Q;
print_r(Q("User")->get()); // select * from User
```

### output with class asscociation

[](#output-with-class-asscociation)

You can also output as class association

```
class User{

}
print_r(Q(User::class)->get()); // select * from User
```

### select with fields and filter

[](#select-with-fields-and-filter)

filter parameter is based on laminas-db where

```
print_r(Q("User")->fields(["user_id","username"])->filter(["type"=>1])->get());
// select user_id,username from User where type=1
```

### select with limit and offset

[](#select-with-limit-and-offset)

```
print_r(Q("User")->limit(10)->offset(0)->get()); // select * from User limit 10 offset 0
```

### select with order

[](#select-with-order)

```
print_r(Q("User")->order("user_id desc")->get()); // select * from User order by user_id desc
```

### populate

[](#populate)

populate is used to select related data from other table, it will auto check the relationship between tables by primary key

```
class UserRole{

}

class User{

}

print_r(Q(User::class)->populate([
    UserRole::class=>[]
])->get());

/*
Array
(
    [0] => User Object
        (
            [username] => admin
            [user_id] => 1
            [UserRole] => Array
                (
                    [0] => UserRole Object
                        (
                            [user_role_id] => 1
                            [user_id] => 1
                            [role] => Administrators
                        )

                )

        )

)
*/
```

Stream wrapper
--------------

[](#stream-wrapper)

By using stream wrapper, you can access the database table as a file

```
use R\DB\Schema;
use R\DB\Stream;

Stream::Register(Schema::Create(), "db");
echo file_get_contents("db://User"); //List all users, User is the table name
// User can also be a class name, it will auto convert to table name
```

### List single record

[](#list-single-record)

```
echo file_get_contents("db://User/1"); //List user with primary key 1
```

### List by fields

[](#list-by-fields)

```
//List all user with fields first_name and last_name
echo file_get_contents("db://User?fields[]=first_name&fields[]=last_name");

//List user with primary key 1 and field username
echo file_get_contents("db://User/1?fields[]=user_id&fields[]=username");
```

### List by filter

[](#list-by-filter)

```
$query=http_build_query([
    "filters"=>[
        "status"=>[
            "eq"=>1
        ]
    ]
]);

echo file_get_contents("db://User?$query"); //List all user with status=1
```

### List by limit and offset

[](#list-by-limit-and-offset)

```
echo file_get_contents("db://User?limit=10&offset=0"); //List first 10 users
```

### Check if table exists

[](#check-if-table-exists)

```
file_exists("db://User"); //return true if table User exists
```

### Rename table

[](#rename-table)

```
rename("db://User","db://User2"); //rename table User to User2
```

### Drop table

[](#drop-table)

```
unlink("db://User2"); //drop table User2
```

Schema Aware
------------

[](#schema-aware)

You can define a static method GetSchema() in your class to define the schema of the table

```
class User implements SchemaAwareInterface{
    public static function GetSchema(){
        return $schema1;
    }
}
```

Class R\\DB\\Model
------------------

[](#class-rdbmodel)

By extends R\\DB\\Model, you can use the following methods to operate the database

```
class User extends R\DB\Model{
}
```

### insert record

[](#insert-record)

```
User::Create([
    "username"=>"user1",
    "first_name"=>"John"
])->save();
```

### get record

[](#get-record)

```
$user = User::Get(1);  // 1 is primary key

$user_not_exists = User::Get(999); // $user_not_exists==null
```

### update record

[](#update-record)

```
$user = User::Get(1); // 1 is primary key
$user->first_name="Mary";
$user->save(); // user record updated
```

### delete record

[](#delete-record)

```
$user = User::Get(1);  // 1 is primary key
$user->delete(); // user record is deleted
```

### query list record

[](#query-list-record)

```
$users = User::Query(["status"=>0]);
print_r($users->toArray());  // list all users status is equal to 0
```

Default driver option
---------------------

[](#default-driver-option)

```
[
    PDO::ATTR_PERSISTENT => true,
    PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES => false
]
```

mysql8 collation
----------------

[](#mysql8-collation)

Due to php pdo default collation not match with mysql8, add the following options

```
$options=[
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci'"
];
```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance47

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity86

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% 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 ~17 days

Recently: every ~34 days

Total

151

Last Release

456d ago

Major Versions

3.13.0 → 4.0.02021-08-12

3.14.0 → 4.5.42022-03-23

3.1.1.2 → 4.6.02022-04-19

v3.x-dev → 4.17.22023-06-26

4.18.0 → 5.0.02023-08-24

PHP version history (9 changes)1.1.1PHP &gt;=5.4

3.0.13PHP &gt;=7.0

3.11.0PHP &gt;=7.3

3.15.0PHP ^8.0

3.1.1.1PHP &gt;=7.0 || ^8.0

4.15.0PHP &gt;=7.4

3.16.0PHP 7.0

3.16.1PHP ^7.0

5.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/30f25555e58060ace793b731c172c739d45e9506154c9611990d2f985f61652b?d=identicon)[mathsgod](/maintainers/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (197 commits)")

---

Tags

laminas-dbpdophp

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mathsgod-r-db/health.svg)

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

###  Alternatives

[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[irazasyed/telegram-bot-sdk

The Unofficial Telegram Bot API PHP SDK

3.3k4.5M84](/packages/irazasyed-telegram-bot-sdk)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6939.5M341](/packages/drupal-core-recommended)

PHPackages © 2026

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