Code Generator
The code generator creates PHP classes based on your database tables, dramatically speeding up development.
What It Generates
The code generator supports two architectural patterns:
Repository Pattern (Default)
- Model - PHP class with properties matching table columns
- Repository - Data access layer with ORM integration
- Service - Business logic layer extending BaseService
- REST API - Complete CRUD endpoints (GET, POST, PUT)
- Functional Tests - Test suite for the CRUD API
- Config - Automatic DI bindings (added automatically with
--save)
ActiveRecord Pattern (with --activerecord)
- Model - PHP class with properties and ActiveRecord trait for direct database operations
- REST API - Complete CRUD endpoints (GET, POST, PUT)
- Functional Tests - Test suite for the CRUD API
Usage
APP_ENV=<environment> composer codegen -- --table=<table_name> <arguments> [options]
composer codegen -- --env=<environment> --table=<table_name> <arguments> [options]
Required
--table=<name>- Database table name
Environment
You can specify the environment in two ways:
- Set the
APP_ENVenvironment variable - Use the
--env=<environment>parameter (overridesAPP_ENV)
Note: At least one method must be used to specify the environment (dev, test, prod).
Arguments (at least one required)
| Argument | Description | Repository Pattern | ActiveRecord Pattern |
|---|---|---|---|
all | Generate all components | ✓ All components | ✓ Model, Rest, Test |
model | Generate Model | ✓ | ✓ (with ActiveRecord trait) |
repo or repository | Generate Repository | ✓ | ✗ Not applicable |
service | Generate Service | ✓ | ✗ Not applicable |
rest | Generate REST controller | ✓ | ✓ |
test | Generate Test | ✓ | ✓ |
Options
--activerecord- Use ActiveRecord pattern instead of a Repository pattern--save- Save generated files to disk (otherwise prints to console)--debug- Show debug information
Examples
Repository Pattern (Default)
Generate all components for the 'users' table using the Repository pattern:
# Using APP_ENV
APP_ENV=dev composer codegen -- --table=users all --save
# Using --env parameter
composer codegen -- --env=dev --table=users all --save
This creates:
src/Model/Users.phpsrc/Repository/UsersRepository.phpsrc/Service/UsersService.phpsrc/Rest/UsersRest.phptests/Rest/UsersTest.php- Automatically adds DI bindings to
config/dev/04-repositories.phpandconfig/dev/05-services.php
Generate only specific components:
APP_ENV=dev composer codegen -- --table=products model rest --save
ActiveRecord Pattern
Generate all components for the 'users' table using the ActiveRecord pattern:
# Using APP_ENV
APP_ENV=test composer codegen -- --table=users all --activerecord --save
# Using --env parameter
composer codegen -- --env=test --table=users all --activerecord --save
This creates:
src/Model/Users.php(with ActiveRecord trait)src/Rest/UsersRest.phptests/Rest/UsersTest.php
Generate only the model:
APP_ENV=test composer codegen -- --table=products model --activerecord --save
Preview Without Saving
Preview the generated REST controller without saving to disk:
APP_ENV=dev composer codegen -- --table=orders rest
composer codegen -- --env=dev --table=orders all --activerecord
Automatic Configuration
When using --save with the Repository pattern, repository and service bindings are automatically added to the configuration files:
- Repositories →
config/dev/04-repositories.php - Services →
config/dev/05-services.php
No manual configuration needed!
Note: ActiveRecord pattern does not require DI bindings since models use the ActiveRecord trait for direct database access.
Example output:
Processing Repository for table users...
File saved in src/Repository/UsersRepository.php
Added use statement for UsersRepository to 04-repositories.php
Added DI binding for UsersRepository to 04-repositories.php
Important Notes
Using --save will overwrite existing files without warning. Be careful when regenerating files you've customized.
After generating REST controllers, remember to:
- Run
composer run openapito update the OpenAPI specification - Run
composer run testto verify the generated tests pass
Customizing Templates
You can modify existing templates or create your own. Templates are located in templates/codegen/ and use the Jinja template engine for PHP.
Available templates:
Repository Pattern:
model.php.jinja- Model class templaterepository.php.jinja- Repository class templateservice.php.jinja- Service class templaterest.php.jinja- REST controller templatetest.php.jinja- Test class template
ActiveRecord Pattern:
modelactiverecord.php.jinja- Model class with ActiveRecord trait templaterestactiverecord.php.jinja- REST controller for ActiveRecord template- Uses the same
test.php.jinjatemplate as a Repository pattern
Template variables available:
className- PascalCase class name (e.g.,UserProfile)tableName- Original table name (e.g.,user_profile)namespace- Project namespacefields- Array of table columns with typesprimaryKeys- Array of primary key fieldsnullableFields- Array of nullable fieldsnonNullableFields- Array of non-nullable, non-PK fields