websitesql / database
The lightweight PHP database framework to accelerate development and reduce complexity.
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:framework
pkg:composer/websitesql/database
Requires
- php: >=8.0
- ext-pdo: *
- catfan/medoo: ^2.1
Suggests
- ext-pdo_dblib: For MSSQL or Sybase database on Linux/UNIX platform
- ext-pdo_mysql: For MySQL or MariaDB database
- ext-pdo_oci: For Oracle database
- ext-pdo_pqsql: For PostgreSQL database
- ext-pdo_sqlite: For SQLite database
- ext-pdo_sqlsrv: For MSSQL database on both Window/Liunx platform
README
A powerful database wrapper library built on top of Medoo, providing a simple and intuitive interface for database operations in PHP applications.
Installation
composer require websitesql/database
Basic Usage
Initialization
// Initialize the database provider $db = new WebsiteSQL\Database\Database([ 'type' => 'mysql', 'host' => 'localhost', 'database' => 'name', 'username' => 'your_username', 'password' => 'your_password' ], '../migrations');
CRUD Operations
Select Data
// Select all records from users table $users = $db->select("users", "*"); // Select with conditions $user = $db->get("users", "*", ["id" => 1]); // Select with JOIN $data = $db->select("posts", [ "[>]users" => ["user_id" => "id"] ], [ "posts.id", "posts.title", "users.username" ], [ "posts.status" => "published", "ORDER" => ["posts.created" => "DESC"] ]);
Insert Data
$db->insert("users", [ "username" => "john_doe", "email" => "john@example.com", "created" => date("Y-m-d H:i:s") ]); // Get last inserted ID $lastId = $db->id();
Update Data
$db->update("users", [ "email" => "new_email@example.com" ], [ "id" => 1 ]);
Delete Data
$db->delete("users", [ "id" => 1 ]);
Migrations
The database library includes a migration system for managing database schema changes:
// Run migrations $db->migrations()->up(); // Rollback the last batch of migrations $db->migrations()->down(); // Rollback all migrations $db->migrations()->reset(); // Refresh migrations (rollback all and run again) $db->migrations()->refresh();
API Reference
Base Operations
query(string $statement, array $map = []): Execute raw SQL queriesexec(string $statement): Execute raw statementcreate(string $table, array $columns, array $options = null): Create a tabledrop(string $table): Drop a tableselect(string $table, array $join, array|string $columns = null, array $where = null): Select dataget(string $table, array $join, array|string $columns = null, array $where = null): Get a single recordinsert(string $table, array $values, string $primaryKey = null): Insert dataupdate(string $table, array $data, array $where = null): Update datadelete(string $table, array $where = null): Delete datareplace(string $table, array $columns, array $where = null): Replace data
Aggregation
count(string $table, array $join = null, string $column = null, array $where = null): Count rowsavg(string $table, array $join, string $column = null, array $where = null): Calculate averagemax(string $table, array $join, string $column = null, array $where = null): Get maximum valuemin(string $table, array $join, string $column = null, array $where = null): Get minimum valuesum(string $table, array $join, string $column = null, array $where = null): Calculate sum
Transactions
action(callable $actions): Execute callback in transactionbeginTransaction(): Begin a transactioncommit(): Commit a transactionrollBack(): Rollback a transaction
Utilities
id(?string $name = null): Get last inserted IDpdo(): Get PDO instancedebug(): Get the last query for debugginglog(): Get query loginfo(): Get database connection infoerror(): Get error informationrand(string $table, array $join = null, array|string $columns = null, array $where = null): Get random recordshas(string $table, array $join, array $where = null): Check if records exist
Migrations
migrations()->up(): Run pending migrationsmigrations()->down(): Rollback the last batch of migrationsmigrations()->reset(): Rollback all migrationsmigrations()->refresh(): Rollback all and run again
License
MIT