ForgeOrm Module
Overview
ForgeOrm is a powerful ORM (Object-Relational Mapping) module that provides an elegant way to interact with your database. It offers a fluent query builder, migration system, and database seeding capabilities. For developers who prefer more explicit and less "magical" database interactions, the framework also provides ForgeExplicitOrm as an alternative.
Query Builder
The Query Builder provides a fluent interface to create and execute database queries:
// Example from DashboardController
$filesRaw = $this->queryBuilder->table('storage')->get();
$bucketsRaw = $this->queryBuilder->table('buckets')->get();
// More complex queries
$users = $this->queryBuilder
->table('users')
->where('active', true)
->orderBy('created_at', 'desc')
->limit(10)
->get();
// Insert operations
$this->queryBuilder->table('buckets')->insert([
'id' => UUID::generate(),
'name' => $bucketName,
'created_at' => Date::now(),
'updated_at' => Date::now(),
]);
Migrations
Migrations allow you to version control your database schema:
// Create a new migration
// Run: php forge.php make:migration create_users_table
class CreateUsersTable extends Migration
{
public function up(): void
{
$this->schema->create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
public function down(): void
{
$this->schema->dropIfExists('users');
}
}
Database Seeding
Seeders help you populate your database with test data:
// Create a new seeder
// Run: php forge.php make:seeder UsersTableSeeder
class UsersTableSeeder extends Seeder
{
public function run(): void
{
$this->db->table('users')->insert([
'id' => UUID::generate(),
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => password_hash('secret', PASSWORD_DEFAULT),
'created_at' => Date::now(),
'updated_at' => Date::now(),
]);
}
}
CLI Commands
ForgeOrm provides several CLI commands to help you manage your database:
# Migration Commands
php forge.php make:migration create_users_table
php forge.php migrate
php forge.php migrate:rollback
php forge.php migrate:reset
php forge.php migrate:refresh
# Seeding Commands
php forge.php make:seeder UsersTableSeeder
php forge.php db:seed
php forge.php db:seed --class=UsersTableSeeder