composer require mathsgod/r-db
Using .env file to setup default database connection
DATABASE_HOSTNAME=
DATABASE_DATABASE=
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_PORT=
DATABASE_CHARSET=
Function Q is a fast way to select data from database.
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
You can also output as class association
class User{
}
print_r(Q(User::class)->get()); // select * from User
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
print_r(Q("User")->limit(10)->offset(0)->get()); // select * from User limit 10 offset 0
print_r(Q("User")->order("user_id desc")->get()); // select * from User order by user_id desc
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
)
)
)
)
*/
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:https://User"); //List all users, User is the table name
// User can also be a class name, it will auto convert to table name
echo file_get_contents("db:https://User/1"); //List user with primary key 1
//List all user with fields first_name and last_name
echo file_get_contents("db:https://User?fields[]=first_name&fields[]=last_name");
//List user with primary key 1 and field username
echo file_get_contents("db:https://User/1?fields[]=user_id&fields[]=username");
$query=http_build_query([
"filters"=>[
"status"=>[
"eq"=>1
]
]
]);
echo file_get_contents("db:https://User?$query"); //List all user with status=1
echo file_get_contents("db:https://User?limit=10&offset=0"); //List first 10 users
file_exists("db:https://User"); //return true if table User exists
rename("db:https://User","db:https://User2"); //rename table User to User2
unlink("db:https://User2"); //drop table User2
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;
}
}
By extends R\DB\Model, you can use the following methods to operate the database
class User extends R\DB\Model{
}
User::Create([
"username"=>"user1",
"first_name"=>"John"
])->save();
$user = User::Get(1); // 1 is primary key
$user_not_exists = User::Get(999); // $user_not_exists==null
$user = User::Get(1); // 1 is primary key
$user->first_name="Mary";
$user->save(); // user record updated
$user = User::Get(1); // 1 is primary key
$user->delete(); // user record is deleted
$users = User::Query(["status"=>0]);
print_r($users->toArray()); // list all users status is equal to 0
[
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false
]
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'"
];