Appearance
labmin/materialize-my-sql
Defines MySQL/MariaDB tables as PHP classes (^0.5), executes validated INSERT / UPDATE statements via DatabaseManager, and manages per-module drizzle-style migrations and idempotent seeds.
bash
composer require labmin/materialize-my-sql:^0.5Schema Attributes (Labmin\MaterializeSQL\Attributes\)
php
use DateTimeImmutable;
use Labmin\MaterializeSQL\Attributes\{Check, Column, ColumnDefault, ColumnType, Index, IndexColumn, IndexOrder, PrimaryKey, Table};
#[Table('order_lines')]
#[PrimaryKey(columns: ['id'])]
#[Index(name: 'order_lines_order_idx', columns: ['orderId'])]
#[Check(name: 'order_lines_qty_chk', expression: 'quantity > 0')]
final class OrderLines
{
#[Column(ColumnType::INT, autoIncrement: true)]
public ?int $id = null;
#[Column(ColumnType::INT, column: 'order_id')]
public int $orderId;
#[Column(ColumnType::DECIMAL, precision: 12, scale: 2)]
public string $amount;
#[Column(ColumnType::TIMESTAMP, column: 'created_at', default: ColumnDefault::CURRENT_TIMESTAMP)]
public ?DateTimeImmutable $createdAt = null;
}#[Table] automatically registers the class as a Materialize #[DataObject]. Public properties without #[Column] are ignored by SQL generation.
#[Column] options
| Option | Purpose |
|---|---|
ColumnType::* | Required storage type (INT, BIGINT, DECIMAL, VARCHAR, TEXT, MEDIUMTEXT, DATE, DATETIME, TIMESTAMP, BOOL, ENUM, JSON, etc.). |
column | Physical SQL column name when different from the PHP camelCase property. |
length, precision, scale | Character length or decimal (M,D) sizing. |
unsigned, zerofill | Numeric modifiers (zerofill requires unsigned: true). |
values | Allowed values for ENUM / SET. |
nullable | SQL NULL vs NOT NULL. |
default | Scalar literal, null, ColumnDefault::CURRENT_TIMESTAMP, or new Expression('...'). |
autoIncrement | Marks auto-increment PK column (omitted automatically on insert). |
onUpdateCurrentTimestamp | Adds ON UPDATE CURRENT_TIMESTAMP. |
generated, stored, comment | Generated column expression (use nullable: true on MariaDB) and SQL comment. |
MaterializeMySql Facade
| Method | Returns | Purpose |
|---|---|---|
MaterializeMySql::insert($schemaOrObj, ?array $data = null, ?DatabaseManager $connection = null) | int | Validates against #[Table] + constraints and runs INSERT; returns auto-increment ID (or 0). |
MaterializeMySql::update($schemaOrObj, ?array $data = null, ?DatabaseManager $connection = null) | int | Validates and runs UPDATE using primary-key properties as WHERE; returns affected rows. |
MaterializeMySql::createTable(string $class, ?DatabaseManager $connection = null) | void | Executes CREATE TABLE. |
MaterializeMySql::buildCreateTableSql / buildInsertSql / buildUpdateSql / tableMetadata | string / metadata | Generates SQL or inspects metadata without touching the database. |
In modules, prefer LIMS\Platform\Repository's $this->insert() and $this->update() wrappers, which forward directly to MaterializeMySql.
Migrations & Seeds
Each module/submodule keeps schemas in Database/Schemas/, migrations in Database/Migrations/ (+ meta/ snapshots), and seeds in Database/Seeds/.
php
use Labmin\MaterializeSQL\Migration\Migrations;
use Labmin\MaterializeSQL\Seeding\Seeds;
$migrations = new Migrations();
$migrations->preview(__DIR__); // Preview pending schema diff
$migrations->generate(__DIR__); // Generate SQL migration (or return rename questions)
$migrations->migrate(__DIR__); // Apply pending migrations
$migrations->pull(__DIR__, ['orders*']); // Adopt existing legacy tables into Schemas/
$migrations->status(__DIR__); // Applied / pending / partial / modified / missing
$migrations->check(__DIR__); // Detect database drift vs migrations
$seeds = new Seeds();
$seeds->preview(__DIR__, 'production');
$seeds->run(__DIR__, 'production'); // Idempotent upsert in one transactionLimsRouter registers MigrationController::for(__DIR__, ...) automatically at ?feature=migrations for Master users.