mathsgod / r-db
A lightweight orm library for mysql
Requires
- php: >=8.0
- ext-pdo: *
- doctrine/annotations: ^1.13
- illuminate/collections: ^9.52|^10.0
- laminas/laminas-db: ^2.18
- laminas/laminas-hydrator: ^4.2
- laminas/laminas-paginator: ^2.11
- league/event: ^3.0
- psr/container: *
- psr/event-dispatcher: ^1.0
- symfony/cache: ^5 | ^6.0
- symfony/validator: ^5.3
- vlucas/phpdotenv: ^5.4
Requires (Dev)
- league/container: ^4.2
- phpunit/phpunit: ^9.6
- thecodingmachine/graphqlite: ^5.0 | ^6.0
- dev-main
- dev-next
- 5.9.0
- 5.8.0
- 5.7.0
- 5.6.4
- 5.6.3
- 5.6.2
- 5.6.1
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.18.0
- 4.17.3
- 4.17.2
- 4.17.1
- 4.17.0
- 4.16.3
- 4.16.2
- 4.16.1
- 4.16.0
- 4.15.1
- 4.15.0
- 4.14.0
- 4.13.1
- 4.13.0
- 4.12.0
- 4.11.1
- 4.11.0
- 4.10.0
- 4.9.5
- 4.9.4
- 4.9.3
- 4.9.2
- 4.9.1
- 4.9.0
- 4.8.4
- 4.8.3
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.1
- 4.7.0
- 4.6.0
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.14
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- v3.x-dev
- 3.16.1
- 3.16.0
- 3.15.2
- 3.15.1
- 3.15.0
- 3.14.0
- 3.13.0
- 3.12.3
- 3.12.2
- 3.12.1
- 3.12.0
- 3.11.0
- 3.10.1
- 3.10.0
- 3.9.0
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.0
- 3.6.12
- 3.6.11
- 3.6.10
- 3.6.9
- 3.6.8
- 3.6.7
- 3.6.6
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1.2
- 3.1.1.1
- 3.1.1
- 3.1.0
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.0
- 1.1.2
- 1.1.1
- 1.1.0
- dev-theorchards
- dev-php-70
This package is auto-updated.
Last update: 2024-12-24 08:49:32 UTC
README
r-db
Install
composer require mathsgod/r-db
Setup
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 is a fast way to select data from database.
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
You can also output as class association
class User{ } print_r(Q(User::class)->get()); // select * from User
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
print_r(Q("User")->limit(10)->offset(0)->get()); // select * from User limit 10 offset 0
select with order
print_r(Q("User")->order("user_id desc")->get()); // select * from User order by user_id desc
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
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
echo file_get_contents("db://User/1"); //List user with primary key 1
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
$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
echo file_get_contents("db://User?limit=10&offset=0"); //List first 10 users
Check if table exists
file_exists("db://User"); //return true if table User exists
Rename table
rename("db://User","db://User2"); //rename table User to User2
Drop table
unlink("db://User2"); //drop table User2
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
By extends R\DB\Model, you can use the following methods to operate the database
class User extends R\DB\Model{ }
insert record
User::Create([ "username"=>"user1", "first_name"=>"John" ])->save();
get record
$user = User::Get(1); // 1 is primary key $user_not_exists = User::Get(999); // $user_not_exists==null
update record
$user = User::Get(1); // 1 is primary key $user->first_name="Mary"; $user->save(); // user record updated
delete record
$user = User::Get(1); // 1 is primary key $user->delete(); // user record is deleted
query list record
$users = User::Query(["status"=>0]); print_r($users->toArray()); // list all users status is equal to 0
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
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'" ];