flytachi/db-mapping

Database Mapping

1.2.6 2025-07-11 08:48 UTC

This package is auto-updated.

Last update: 2025-08-11 09:04:56 UTC


README

Overview

This library provides a robust and flexible solution for mapping PHP classes and their properties to database schemas. It leverages PHP 8 attributes to define database table structures, column types, constraints, and relationships directly within your model classes. This approach reduces boilerplate code, improves maintainability, and ensures a clear, declarative definition of your database schema.

Features

  • Attribute-based Schema Definition: Define database tables, columns, and constraints using PHP 8 attributes.
  • Support for Various Data Types: Comprehensive support for common SQL data types, including integers, strings, booleans, dates, and more.
  • Indexing and Constraints: Easily define primary keys, unique keys, and foreign key constraints.
  • Extensible Architecture: Designed for easy extension to support custom attributes and database dialects.
  • SQL Generation: Automatically generates SQL DDL (Data Definition Language) statements based on your class definitions.

Installation

To install the DB Mapping Library, you can use Composer:

composer require flytachi/db-mapping

Usage

Defining a Table

To define a database table, you can use the #[Table] attribute on your PHP class. Each property in the class can then be annotated with various attributes to define its corresponding database column.

<?php

declare(strict_types=1);

namespace App\Models;

use Flytachi\DbMapping\Attributes\Entity\Table;
use Flytachi\DbMapping\Attributes\Hybrid\Id;
use Flytachi\DbMapping\Attributes\Primal\Varchar;
use Flytachi\DbMapping\Attributes\Primal\Integer;
use Flytachi\DbMapping\Attributes\Additive\DefaultVal;
use Flytachi\DbMapping\Attributes\Additive\NullableIs;

#[Table(name: "users")]
class User
{
    #[Id]
    #[Integer]
    public int $id;

    #[Varchar(length: 255)]
    public string $name;

    #[Varchar(length: 255)]
    #[NullableIs(true)]
    #[DefaultVal("guest@example.com")]
    public ?string $email;

    #[Integer]
    public int $age;
}

Generating SQL

You can use the library to generate SQL DDL statements from your defined classes. (Further examples will be provided in docs.md)

Contributing

Contributions are welcome! Please see CONTRIBUTING.md (if available) for details on how to contribute.

License

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

Generated by Manus AI