enduron/ormcompiler

ORMcompiler for Enduron PHP Framework

Installs: 72

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

Type:script

pkg:composer/enduron/ormcompiler

1.2.0 2025-09-06 16:54 UTC

This package is auto-updated.

Last update: 2025-12-02 14:27:22 UTC


README

A powerful ORM (Object-Relational Mapping) code generator for PHP 8.2+ that automatically generates type-safe database access classes from your database schema.

License: MIT PHP Version

Overview

The Enduron ORM Compiler analyzes your database structure and generates clean, efficient PHP classes for database operations. It creates a complete data access layer including model classes, DAOs (Data Access Objects), query builders, and optionally TypeScript/JavaScript classes for frontend integration.

Features

  • Automatic Code Generation: Generates PHP classes from existing database schemas
  • Type-Safe Operations: Full PHP 8.2+ type hints and nullable types support
  • Multiple Database Support: Works with MySQL/MySQLi via PDO or native drivers
  • Relationship Mapping: Automatic detection and handling of foreign keys and many-to-many relationships
  • Frontend Integration: Optional TypeScript/JavaScript class generation
  • SET/ENUM Support: Automatically generates constants and validation for SET and ENUM fields
  • UUID Support: Built-in support for UUID primary keys
  • Query Builder: Fluent interface for complex database queries
  • Namespace Support: PSR-4 compliant namespace generation
  • Web & CLI Interface: Use via browser-based GUI or command-line

Requirements

Installation

For full framework integration:

composer create-project enduron/framework-base

Install via Composer:

composer require enduron/ormcompiler

Quick Start

Command Line Usage

Create a .env file with your database configuration:

DB_DRIVER=mysqli
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=your_database
DB_USERNAME=your_username
DB_PASSWORD=your_password
ORMCOMPILER_OUTPUT_MODELS=./generated/models
ORMCOMPILER_OUTPUT_JS=./generated/js

Run the compiler:

php run.php

Web Interface

Start the built-in web interface:

php -S localhost:8000 index.php

Navigate to http://localhost:8000 in your browser to configure and run the compiler.

Generated Code Structure

For each database table, the compiler generates:

PHP Classes

  • Model Class ([Table]Object.php) - Represents a database record with getters/setters
  • DAO Class ([Table]DAO.php) - Data Access Object with CRUD operations
  • Query Class ([Table]Query.php) - Fluent query builder
  • Manager Classes - Collection and list management

TypeScript/JavaScript Classes (Optional)

  • Object Class - Frontend data model
  • DAO Class - API communication layer

Example Generated Code

// UserObject.php - Model class
class UserObject extends BaseObject {
    private ?int $id = null;
    private ?string $username = null;
    private ?string $email = null;
    private ?string $status = null;

    public function getId(): ?int { return $this->id; }
    public function setId(?int $id): self { $this->id = $id; return $this; }
    // ... more getters/setters
}

// UserDAO.php - Data Access Object
class UserDAO extends BaseDAO {
    // Constants for ENUM field 'status'
    public const STATUS_ACTIVE = 'active';
    public const STATUS_INACTIVE = 'inactive';
    public const STATUS_VALUES = ['active', 'inactive'];

    public function findById(int $id): ?UserObject { /* ... */ }
    public function save(UserObject $user): bool { /* ... */ }
    public function delete(UserObject $user): bool { /* ... */ }
    // ... more methods
}

SET and ENUM Field Support

The compiler automatically generates constants for SET and ENUM database fields:

For a field: status ENUM('active', 'inactive', 'pending')

PHP generates:

public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
public const STATUS_PENDING = 'pending';
public const STATUS_VALUES = ['active', 'inactive', 'pending'];

TypeScript generates:

public static readonly STATUS_ACTIVE: string = 'active';
public static readonly STATUS_INACTIVE: string = 'inactive';
public static readonly STATUS_PENDING: string = 'pending';
public static readonly STATUS_VALUES: string[] = ['active', 'inactive', 'pending'];

Configuration Options

The compiler supports various configuration options:

  • Namespace Support: Enable/disable PSR-4 namespace generation
  • UUID Primary Keys: Use UUIDs instead of auto-increment integers
  • Table Updates: Automatically update table structures
  • Driver Selection: Choose between PDO and native database drivers
  • Output Paths: Separate paths for models, DAOs, JavaScript classes, etc.
  • Foreign Key Handling: Automatic relationship detection and mapping

Architecture

Core Components

  • ORMCompiler - Main orchestrator that analyzes schema and coordinates generation
  • ORMConfig - Configuration management
  • Builder Classes - Specialized code generators for different class types
  • Creole Layer - Database abstraction for cross-database compatibility
  • Template System - Base class templates for generated code

Integration with Enduron Framework

The ORM Compiler is part of the Enduron Framework ecosystem:

  • enduron/core - Provides base classes, utilities, and database abstraction (required)
  • enduron/framework-base - Complete framework with routing, middleware, and more

Use Cases

  • Rapid Application Development: Generate complete data access layer in seconds
  • Database-First Development: Start with existing database schemas
  • API Development: Generate type-safe models for REST/GraphQL APIs
  • Legacy Database Integration: Modernize access to legacy databases
  • Full-Stack Development: Generate matching PHP and TypeScript classes

Documentation

For detailed documentation, visit:

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Mario Kaufmann Email: m.kaufmann@bueroparallel.de

Links

Made with ❤️ for the PHP community