{"id":18486206,"url":"https://github.com/mathsgod/r-db","last_synced_at":"2026-03-02T09:02:31.555Z","repository":{"id":62525036,"uuid":"164818501","full_name":"mathsgod/r-db","owner":"mathsgod","description":"A lightwieght orm library for mysql","archived":false,"fork":false,"pushed_at":"2025-05-14T07:35:36.000Z","size":1157,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"next","last_synced_at":"2025-10-08T05:43:54.054Z","etag":null,"topics":["laminas-db","pdo","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathsgod.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-01-09T08:12:20.000Z","updated_at":"2025-05-14T07:35:40.000Z","dependencies_parsed_at":"2024-03-14T03:31:09.085Z","dependency_job_id":"43d09c97-bfd5-4414-a78f-c3f2d31c5618","html_url":"https://github.com/mathsgod/r-db","commit_stats":{"total_commits":122,"total_committers":1,"mean_commits":122.0,"dds":0.0,"last_synced_commit":"2c3ce7a209c24affb6faee54f93259d135318c83"},"previous_names":[],"tags_count":150,"template":false,"template_full_name":null,"purl":"pkg:github/mathsgod/r-db","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Fr-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Fr-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Fr-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Fr-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathsgod","download_url":"https://codeload.github.com/mathsgod/r-db/tar.gz/refs/heads/next","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Fr-db/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29996277,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laminas-db","pdo","php"],"created_at":"2024-11-06T12:48:18.919Z","updated_at":"2026-03-02T09:02:31.472Z","avatar_url":"https://github.com/mathsgod.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PHP Composer](https://github.com/mathsgod/r-db/actions/workflows/php.yml/badge.svg)](https://github.com/mathsgod/r-db/actions/workflows/php.yml)\n\n[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/mathsgod/r-db)\n\n# r-db\n\n## Install\n```\ncomposer require mathsgod/r-db\n```\n\n## Setup\n### using .env\nUsing .env file to setup default database connection\n```ini\nDATABASE_HOSTNAME=\nDATABASE_DATABASE=\nDATABASE_USERNAME=\nDATABASE_PASSWORD=\nDATABASE_PORT=\nDATABASE_CHARSET=\n```\n\n## Function Q\nFunction Q is a fast way to select data from database.\n### simple select\n\nThis will select all data from table User and output as array of stdClass\n```php\nuse function R\\DB\\Q;\nprint_r(Q(\"User\")-\u003eget()); // select * from User\n\n```\n\n### output with class asscociation\n\nYou can also output as class association\n\n```php\nclass User{\n\n}\nprint_r(Q(User::class)-\u003eget()); // select * from User\n```\n\n\n### select with fields and filter\nfilter parameter is based on laminas-db where \n\n```php\nprint_r(Q(\"User\")-\u003efields([\"user_id\",\"username\"])-\u003efilter([\"type\"=\u003e1])-\u003eget()); \n// select user_id,username from User where type=1\n\n```\n\n### select with limit and offset\n```php\nprint_r(Q(\"User\")-\u003elimit(10)-\u003eoffset(0)-\u003eget()); // select * from User limit 10 offset 0\n```\n\n### select with order\n```php\nprint_r(Q(\"User\")-\u003eorder(\"user_id desc\")-\u003eget()); // select * from User order by user_id desc\n```\n\n### populate\npopulate is used to select related data from other table, it will auto check the relationship between tables by primary key\n```php \nclass UserRole{\n\n}\n\nclass User{\n\n}\n\nprint_r(Q(User::class)-\u003epopulate([\n    UserRole::class=\u003e[]\n])-\u003eget());\n\n/* \nArray\n(\n    [0] =\u003e User Object\n        (\n            [username] =\u003e admin\n            [user_id] =\u003e 1\n            [UserRole] =\u003e Array\n                (\n                    [0] =\u003e UserRole Object\n                        (\n                            [user_role_id] =\u003e 1\n                            [user_id] =\u003e 1\n                            [role] =\u003e Administrators\n                        )\n\n                )\n\n        )\n\n)\n*/\n```\n\n## Stream wrapper\n\nBy using stream wrapper, you can access the database table as a file\n\n\n```php\nuse R\\DB\\Schema;\nuse R\\DB\\Stream;\n\nStream::Register(Schema::Create(), \"db\");\necho file_get_contents(\"db://User\"); //List all users, User is the table name\n// User can also be a class name, it will auto convert to table name\n```\n\n### List single record\n```php\necho file_get_contents(\"db://User/1\"); //List user with primary key 1\n``` \n\n### List by fields\n```php\n//List all user with fields first_name and last_name\necho file_get_contents(\"db://User?fields[]=first_name\u0026fields[]=last_name\"); \n\n//List user with primary key 1 and field username\necho file_get_contents(\"db://User/1?fields[]=user_id\u0026fields[]=username\"); \n```\n\n\n### List by filter\n```php\n\n$query=http_build_query([\n    \"filters\"=\u003e[\n        \"status\"=\u003e[\n            \"eq\"=\u003e1\n        ]\n    ]\n]);\n\necho file_get_contents(\"db://User?$query\"); //List all user with status=1\n\n```\n\n### List by limit and offset\n```php\necho file_get_contents(\"db://User?limit=10\u0026offset=0\"); //List first 10 users\n```\n\n### Check if table exists\n```php\nfile_exists(\"db://User\"); //return true if table User exists\n```\n\n### Rename table\n```php\nrename(\"db://User\",\"db://User2\"); //rename table User to User2\n```\n\n### Drop table\n```php\nunlink(\"db://User2\"); //drop table User2\n```\n\n## Schema Aware\nYou can define a static method GetSchema() in your class to define the schema of the table\n```php\nclass User implements SchemaAwareInterface{\n    public static function GetSchema(){\n        return $schema1;\n    }\n}\n\n\n```\n## Class R\\DB\\Model\nBy extends R\\DB\\Model, you can use the following methods to operate the database\n\n```php\nclass User extends R\\DB\\Model{\n} \n```\n\n\n### insert record\n```php\n\nUser::Create([\n    \"username\"=\u003e\"user1\",\n    \"first_name\"=\u003e\"John\"\n])-\u003esave();\n```\n\n### get record\n```php\n$user = User::Get(1);  // 1 is primary key\n\n$user_not_exists = User::Get(999); // $user_not_exists==null\n```\n\n### update record\n```php\n$user = User::Get(1); // 1 is primary key\n$user-\u003efirst_name=\"Mary\";\n$user-\u003esave(); // user record updated\n```\n\n### delete record\n```php\n$user = User::Get(1);  // 1 is primary key\n$user-\u003edelete(); // user record is deleted\n```\n\n### query list record\n```php\n$users = User::Query([\"status\"=\u003e0]);\nprint_r($users-\u003etoArray());  // list all users status is equal to 0\n```\n\n## Default driver option\n```php\n[\n    PDO::ATTR_PERSISTENT =\u003e true,\n    PDO::ATTR_ERRMODE =\u003e PDO::ERRMODE_SILENT,\n    PDO::ATTR_DEFAULT_FETCH_MODE =\u003e PDO::FETCH_ASSOC,\n    PDO::ATTR_EMULATE_PREPARES =\u003e false\n]\n```\n\n## mysql8 collation\nDue to php pdo default collation not match with mysql8, add the following options\n```php\n$options=[\n    PDO::MYSQL_ATTR_INIT_COMMAND =\u003e \"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci'\"\n];\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathsgod%2Fr-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathsgod%2Fr-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathsgod%2Fr-db/lists"}