Generate PHP Class files according to your Database structure with modern PHP 8.1+ syntax
- PHP 8.1 or higher
- MySQL/MariaDB database
- PDO extension
composer require ngfw/database-to-class- Clone or download this repository
- Run
composer installto generate autoloader - Include the autoloader:
require 'vendor/autoload.php';
- Edit
dbconfig.phpto configure your database connection - Make sure
GeneratedClassesdirectory is writable
chmod 777 GeneratedClasses<?php
require 'vendor/autoload.php';
use DatabaseToClass\ClassGenerator;
$generator = new ClassGenerator();
$generator->setTable('users');
$classCode = $generator->buildClass();
file_put_contents('GeneratedClasses/users.php', $classCode);The Classes/ directory still works for backward compatibility:
<?php
include('Classes/ClassGenerator.php');
$generator = new ClassGenerator();
// ... rest of your codeThe new CLI uses Symfony Console for a modern experience with colors, progress bars, and interactive prompts.
Interactive mode (select table from list):
php generate.phpList all tables:
php generate.php list-tablesGenerate specific table:
php generate.php generate usersGenerate all tables:
php generate.php generate --allCustom output directory:
php generate.php generate users --output=app/Models- π¨ Colored output for better readability
- π Progress bars for bulk generation
- π Interactive table selection
- β Validation of table primary keys
- π Relationship detection display
if you webserver is not already pointing to Directory where files are located, you can simply run:
\>$ php -S localhost:8080then open you browser and navigate to http://localhost:8080
Web interface will also generate class usage documentation
Professional command-line interface with rich features:
- Colored output: Easy-to-read success/error messages
- Interactive prompts: Select tables from a list
- Progress bars: Visual feedback for bulk operations
- Table listing: View all tables with status indicators
- Flexible options: Generate single table, all tables, or custom output directory
- Helpful commands: Built-in help and command descriptions
Generated classes use cutting-edge PHP features:
- Typed properties for better IDE support and type safety
- Union types (e.g.,
int|string,bool|int) - Strict types declaration for compile-time type checking
- Modern array syntax and null coalescing operators
- Return type declarations on all methods
Build complex queries with an elegant, chainable syntax:
Available Methods:
where($column, $operator, $value)- Add WHERE clauseorWhere($column, $operator, $value)- Add OR WHERE clausewhereIn($column, $values)- Add WHERE IN clauseorderBy($column, $direction)- Add ORDER BY clausetake($limit)- Limit number of resultsskip($offset)- Skip number of recordsget()- Execute query and get all resultsfirst()- Get first result onlycount()- Count matching records
Examples:
<?php
$user = include("GeneratedClasses/users.php");
// Simple WHERE query
$activeUsers = $user->where('status', 'active')->get();
// Multiple WHERE conditions
$results = $user->where('age', '>', 18)
->where('status', 'active')
->get();
// OR conditions
$results = $user->where('role', 'admin')
->orWhere('role', 'moderator')
->get();
// WHERE IN
$results = $user->whereIn('id', [1, 2, 3, 5, 8])->get();
// ORDER BY and LIMIT
$topUsers = $user->where('status', 'active')
->orderBy('points', 'DESC')
->take(10)
->get();
// Pagination
$page2 = $user->orderBy('created_at', 'DESC')
->skip(20)
->take(10)
->get();
// Get first result
$admin = $user->where('role', 'admin')->first();
// Count records
$activeCount = $user->where('status', 'active')->count();
// Complex queries
$results = $user->where('age', '>=', 21)
->where('status', 'active')
->whereIn('country', ['US', 'CA', 'UK'])
->orderBy('last_login', 'DESC')
->take(50)
->get();Supported Operators:
=,!=,<>- Equality>,>=,<,<=- ComparisonLIKE- Pattern matchingIN- List membership (viawhereIn())
Generated classes include lifecycle hooks that allow you to run custom logic before and after CRUD operations:
Available Hooks:
beforeValidate()- Run logic before validationafterValidate()- Run logic after successful validationbeforeAdd()- Modify data or cancel insert (returnfalseto cancel)afterAdd($insertedId)- Run logic after successful insertbeforeUpdate()- Modify data or cancel update (returnfalseto cancel)afterUpdate()- Run logic after successful updatebeforeDelete($id)- Cancel delete operation (returnfalseto cancel)afterDelete($id)- Run logic after successful delete
Example - Auto-timestamp:
<?php
// Extend the generated class
class User extends users {
protected function beforeAdd(): bool {
// Auto-set created_at timestamp
$this->created_at = date('Y-m-d H:i:s');
return true;
}
protected function beforeUpdate(): bool {
// Auto-set updated_at timestamp
$this->updated_at = date('Y-m-d H:i:s');
return true;
}
protected function afterDelete(int|string $id): void {
// Log deletion
error_log("User {$id} was deleted at " . date('Y-m-d H:i:s'));
}
}Example - Prevent deletion:
protected function beforeDelete(int|string $id): bool {
// Don't allow deleting admin users
if ($this->role === 'admin') {
return false; // Cancel the delete
}
return true;
}Generated classes include built-in validation based on your database schema:
- Type validation: Ensures integers are integers, numbers are numeric, etc.
- Length validation: Enforces VARCHAR and CHAR length constraints
- Required field validation: Checks NOT NULL columns
- ENUM validation: Validates against allowed enum values
- Easy error handling: Get detailed validation errors
$user = include("GeneratedClasses/users.php");
$user->email = "invalid-email-that-is-way-too-long-for-the-database-column";
$user->age = "not a number";
if (!$user->validate()) {
print_r($user->getValidationErrors());
// Array
// (
// [email] => Array([0] => email exceeds maximum length of 255)
// [age] => Array([0] => age must be an integer)
// )
} else {
$user->add();
}The generator automatically detects foreign key relationships and generates methods for easy data access:
- BelongsTo Relationships: When your table has a foreign key to another table
- HasMany Relationships: When other tables have foreign keys pointing to your table
- BelongsToMany Relationships: Many-to-many relationships through junction/pivot tables
If you have a posts table with a user_id foreign key to users table:
// BelongsTo: Get the user who created a post
$post = include("GeneratedClasses/posts.php");
$postData = $post->get_id(1);
foreach($postData[0] as $key => $value) {
$post->{$key} = $value;
}
$author = $post->user(); // Returns the related user record
// HasMany: Get all posts by a user
$user = include("GeneratedClasses/users.php");
$userData = $user->get_id(1);
foreach($userData[0] as $key => $value) {
$user->{$key} = $value;
}
$posts = $user->posts(10); // Returns up to 10 posts by this userThe generator automatically detects junction tables and creates many-to-many relationships. For example, with users, roles, and a user_roles junction table:
Database Structure:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE roles (
id INT PRIMARY KEY,
name VARCHAR(255)
);
CREATE TABLE user_roles (
user_id INT,
role_id INT,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (role_id) REFERENCES roles(id)
);Usage:
<?php
$user = include("GeneratedClasses/users.php");
$role = include("GeneratedClasses/roles.php");
// Load a user
$userData = $user->get_id(1);
foreach($userData[0] as $key => $value) {
$user->{$key} = $value;
}
// Get all roles for this user
$userRoles = $user->roles(); // Returns array of role records
// Attach a role to the user
$user->attachRoles(2); // Attach role with ID 2
// Detach a role from the user
$user->detachRoles(2); // Remove role with ID 2
// Sync roles (removes all existing, adds new ones)
$user->syncRoles([1, 3, 5]); // User will have only roles 1, 3, and 5
// Works both ways - get users for a role
$roleData = $role->get_id(1);
foreach($roleData[0] as $key => $value) {
$role->{$key} = $value;
}
$roleUsers = $role->users(); // Returns array of user records with this roleGenerated Methods:
roles()orusers()- Retrieve related records through junction tableattachRoles($id)- Add a relationship (prevents duplicates)detachRoles($id)- Remove a relationshipsyncRoles($ids)- Replace all relationships with new set
The generated documentation will show all detected relationships and how to use them.
Have fun
