Download the PHP package kdevhubin/pdoentitygenerator without Composer
On this page you can find all versions of the php package kdevhubin/pdoentitygenerator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kdevhubin/pdoentitygenerator
More information about kdevhubin/pdoentitygenerator
Files in kdevhubin/pdoentitygenerator
Package pdoentitygenerator
Short Description CLI tool to generate PHP PDO Entity and Repository classes from database tables
License MIT
Informations about the package pdoentitygenerator
PDO Entity Generator
A CLI tool to generate PHP PDO Entity and Repository classes from database tables. Works in any PHP project — no framework required.
Table of Contents
- Requirements
- Getting Started
- Installation
- Configuration
- Quick Start
- Usage
- Generating Entities
- Generated Entity Example
- Generated Repository Example
- Generated PdoFactory
- Using Generated Classes
- Reference
- Configuration Options
- Naming Conventions
- Type Mapping
- Architecture
- Contributing
- Development Setup
- Project Structure
- Coding Standards
- Adding a New SQL Type Mapping
- Modifying Generated Code Templates
- Testing Changes Locally
- Submitting Changes
- Troubleshooting
- Licence
Requirements
- PHP 8.4 or higher
- PDO extension (
ext-pdo) - MySQL / MariaDB database
- Composer 2.x
Getting Started
Installation
Install the package via Composer:
This package is a Composer Plugin. On first install, Composer will prompt you to allow the plugin:
Type y to allow it. You can also pre-authorise the plugin by adding it to your project's composer.json:
Once allowed, the plugin automatically creates config/pdoentitygenerator.yaml with default settings. There is no manual setup step required.
Configuration
Open config/pdoentitygenerator.yaml in your project root and update it with your database credentials:
Important: The
database.dbnamefield is required. The generator will not run without it.
Quick Start
Usage
Generating Entities
Run the CLI command with the table subcommand followed by the database table name:
The tool will:
- Read
config/pdoentitygenerator.yamlfor database connection details - Connect to the database via PDO
- Inspect the table schema (column names, types, nullability)
- Generate an Entity class, a Repository class, and a PdoFactory class in the configured output directories
Example
This generates:
src/Entity/TestMyTable.php— POPO with typed properties, getters, and setterssrc/Repository/TestMyTableRepository.php— PDO-based CRUD repositorysrc/Factory/PdoFactory.php— framework-agnostic factory for creating configured PDO connections (skipped if already exists)
Generated Entity Example
For a table test_my_table with columns id, name, email, and created_at:
Generated Repository Example
CRUD Methods
| Method | Description | Return Type |
|---|---|---|
find($id) |
Finds a single entity by primary key | ?Entity (null if not found) |
findAll() |
Returns all entities from the table | Entity[] |
insert($entity) |
Inserts a new record and sets the generated ID on the entity | Entity |
update($entity) |
Updates an existing record by primary key | Entity |
delete($id) |
Deletes a record by primary key | bool |
All queries use prepared statements with bindValue() and explicit PDO::PARAM_* type constants to enforce data types and prevent SQL injection.
Generated PdoFactory
The generator also creates a PdoFactory class that reads your database credentials from config/pdoentitygenerator.yaml and returns a configured PDO instance. Uses a singleton pattern — subsequent calls to create() return the same connection, avoiding redundant connections. Call reset() to force a new connection. This is framework-agnostic and works in any PHP project.
Plain PHP Usage
Symfony Usage (services.yaml)
Register the factory as a service to enable autowiring for all repositories:
Using Generated Classes
Reference
Configuration Options
| Key | Description | Default |
|---|---|---|
database.host |
Database host | 127.0.0.1 |
database.port |
Database port | 3306 |
database.dbname |
Default database name (required) | — |
database.username |
Database username | root |
database.password |
Database password | — |
database.driver |
PDO driver | mysql |
output.entity_namespace |
Namespace for generated entities | App\Entity |
output.repository_namespace |
Namespace for generated repositories | App\Repository |
output.factory_namespace |
Namespace for generated factory class | App\Factory |
output.entity_directory |
Output directory for entity files | src/Entity |
output.repository_directory |
Output directory for repository files | src/Repository |
output.factory_directory |
Output directory for factory file | src/Factory |
Naming Conventions
The generator automatically converts between database snake_case and PHP naming conventions:
| Database (snake_case) | PHP (camelCase / PascalCase) | Used For |
|---|---|---|
test_my_table |
TestMyTable |
Class name |
created_at |
createdAt |
Property name |
first_name |
firstName |
Property name |
id |
id |
Property name |
Type Mapping
| SQL Type | PHP Type |
|---|---|
int, integer, bigint, smallint, mediumint, tinyint |
int |
varchar, char, text, tinytext, mediumtext, longtext, enum, set, json |
string |
decimal, float, double, numeric, real |
float |
datetime, timestamp, date |
\DateTimeImmutable |
tinyint(1), boolean, bool |
bool |
time |
string |
year |
int |
Architecture
How It Works
- CLI Entry Point (
bin/pdoentitygenerator) — Resolves the Composer autoloader and delegates toGenerateEntityCommand - Command (
GenerateEntityCommand) — Parsesargv, loads config, connects to the database, and orchestrates the generation pipeline - Config Loader (
ConfigLoader) — Readsconfig/pdoentitygenerator.yaml, merges with defaults, and validates required fields - Table Inspector (
TableInspector) — ExecutesDESCRIBE <table>to retrieve column metadata (name, type, nullability, primary key) - Entity Generator (
EntityGenerator) — Produces a POPO class with typed properties, getters, setters, and snake_case→camelCase conversion - Repository Generator (
RepositoryGenerator) — Produces a repository class withfind,findAll,insert,update,deletemethods usingbindValue()with explicitPDO::PARAM_*type constants - PdoFactory Generator (
PdoFactoryGenerator) — Produces a framework-agnostic factory class that reads database credentials fromconfig/pdoentitygenerator.yamland returns a configuredPDOinstance - Post-Install Handler (
PostInstallHandler) — Composer Plugin (implementsPluginInterfaceandEventSubscriberInterface) that subscribes toPOST_INSTALL_CMDandPOST_UPDATE_CMDevents to auto-createconfig/pdoentitygenerator.yamlin the host project when the package is installed
Contributing
Development Setup
-
Clone the repository:
-
Install dependencies:
-
Set up a test database (MySQL/MariaDB):
-
Configure the tool:
Create
config/pdoentitygenerator.yamlwith your test database credentials: - Run the generator to verify everything works:
Project Structure
| Directory / File | Purpose |
|---|---|
bin/pdoentitygenerator |
CLI entry point — edit to change autoloader resolution logic |
src/Command/ |
CLI command handling and argument parsing |
src/Composer/ |
Composer Plugin — subscribes to install/update events to auto-create config |
src/Config/ |
Configuration loading and validation |
src/Database/ |
Database introspection (schema reading) |
src/Generator/ |
Code generation — Entity, Repository, and PdoFactory templates |
Coding Standards
This project follows the PHP Coding Standards and Best Practices document. Key rules are summarised below.
General
- PHP Version: All code must target PHP 8.4+
- Strict Types: Every PHP file must declare
declare(strict_types=1); - PSR-12: Follow PSR-12 Extended Coding Style
- Line Length: Maximum 120 characters per line
- No Doctrine: This project intentionally avoids Doctrine ORM — all database interaction uses raw PDO
PHPDoc
- All classes must have a class-level PHPDoc with
@packagetag - All methods must have PHPDoc with
@param,@return, and@throwstags - Document all exception types the method can throw, including those from called methods
Imports and Namespaces
- All classes use the
kdevhub\PdoEntityGenerator\namespace - Replace Fully Qualified Names (FQN) with
useimport statements (e.g.use RuntimeException;instead of\RuntimeException) - Alphabetise import statements
- Remove unused imports
Class Structure
- Mark classes as
finalunless they are designed for extension - Declare visibility on all properties, methods, and constants (
public,protected,private) - Order class members: constants → properties → constructor → public methods → protected methods → private methods
PHP 8.4 Features
- Constructor Promotion: Use constructor property promotion where appropriate
- Readonly Properties: Use
readonlyfor properties that should not change after construction - Match Expressions: Prefer
matchoverswitchfor value-based branching - Typed Constants: Use typed constants (
const string,const array)
Strings and Arrays
- Use single quotes for strings without variable interpolation
- Use
{$variable}syntax for complex interpolation in double-quoted strings - Use short array syntax (
[]) with trailing commas in multiline arrays
Best Practices
- Use early returns (guard clauses) to reduce nesting — maximum 3–4 levels of indentation
- Use named constants instead of magic numbers or strings
- Use dependency injection — avoid
newinside classes for dependencies - All database queries must use parameterised prepared statements
Adding a New SQL Type Mapping
To add support for a new SQL data type:
- Open
src/Database/TableInspector.php -
Add the mapping to the
TYPE_MAPconstant: - If the type requires special handling in generated code (e.g. custom hydration or parameter binding), update the corresponding
matchexpressions in:src/Generator/RepositoryGenerator.php→buildHydrateBody()for reading from DBsrc/Generator/RepositoryGenerator.php→buildBindValueLine()for writing to DBsrc/Generator/RepositoryGenerator.php→resolvePdoParamType()if a new PDO parameter type is needed
Modifying Generated Code Templates
The generated Entity and Repository code is built using PHP heredoc templates within the generator classes:
- Entity template:
src/Generator/EntityGenerator.php→buildClassTemplate(),buildGetter(),buildSetter() - Repository template:
src/Generator/RepositoryGenerator.php→generate(),buildInsertMethod(),buildUpdateMethod(),buildHydrateBody() - PdoFactory template:
src/Generator/PdoFactoryGenerator.php→generate()
When modifying templates:
- Pay close attention to heredoc indentation — the closing marker's position determines the base indent level
- Variable interpolation within heredocs uses
{$variable}syntax - Escape
$with\$when the dollar sign should appear in generated output - Test with tables containing various column types (int, varchar, datetime, boolean) to verify output
Testing Changes Locally
After making changes, verify the generator produces valid output:
Submitting Changes
-
Create a feature branch from
test-generator: - Make your changes and verify generated output
- Ensure all PHPDoc comments are complete with
@param,@return, and@throwstags - Verify imports are alphabetised and no FQNs are used in code
- Commit with a descriptive message
- Push and create a pull request targeting
test-generator
Troubleshooting
"Configuration file not found" error
The config/pdoentitygenerator.yaml file was not created during installation. This usually happens when the Composer Plugin was not allowed. Either:
- Re-run
composer installand typeywhen prompted to trust the plugin, or - Manually create the file:
Then create config/pdoentitygenerator.yaml with the contents shown in the Configuration section.
"Database name must be specified" error
The database.dbname field in config/pdoentitygenerator.yaml is empty or missing. Update it with your database name.
"Database connection failed" error
Verify your database credentials in config/pdoentitygenerator.yaml. Ensure:
- The database server is running
- The host, port, username, and password are correct
- The specified database exists
- The PDO driver matches your database (e.g.
mysqlfor MySQL/MariaDB)
"Table not found or has no columns" error
The specified table does not exist in the configured database. Check:
- The table name is spelled correctly (case-sensitive)
- You are connected to the correct database
Generated files have incorrect namespace
Update the output.entity_namespace, output.repository_namespace, and output.factory_namespace values in config/pdoentitygenerator.yaml to match your project's PSR-4 autoload configuration.
Licence
This project is licensed under the MIT Licence — see the LICENSE file for details.
All versions of pdoentitygenerator with dependencies
ext-pdo Version *
composer-plugin-api Version ^2.0
symfony/yaml Version ^8.0