Članci s tagom #database
-
Duplicate invoice number handling in production: graceful recovery without data loss
fiskalizacija_invoices UNIQUE KEY uq_company_invoice_number_year (company_id, invoice_number, issue_year GENERATED). generateInvoiceNumber MAX over all types including proformas (prevents proforma gap). PDOException catch on constraint name → 409 + 'invoice may already exist' (double-submit defence, no data loss). issue_year GENERATED ALWAYS AS YEAR(issue_date) STORED — same invoice number legal in different years.
-
PHP query builder: building type-safe SQL without an ORM
Shinobi Apps framework QueryBuilder: fluent interface over PDO, all params bound automatically (zero interpolation). ALLOWED_OPERATORS allowlist for where/orWhere/having. validateQualifiedColumnName regex for orderBy/groupBy (prevents backtick injection). whereIn empty array → 1=0 sentinel (never returns all rows). select() trusted-caller design (no user input). aggregate helpers (sum/avg/max/min) validate column before SQL fragment.
-
Active Record in PHP without an ORM: how the Shinobi Apps framework's Model works
An Active Record implementation with QueryBuilder, typed attribute casts, soft delete, eager loading with whereIn batching, and model lifecycle events — without Doctrine, Eloquent, or any Composer package. What zero-dependency means for the database layer.
-
PHP migration system: forward-only, one file per change, shared-host compatible
SQL-only migrations, no PHP interface. Forward-only with no down() method. One logical change per file. Checksum verification on re-run. Shared-host compatible: no information_schema access needed, MySQL 5.5+ syntax only. Works across multiple databases in one command.
-
PHP session security: database-backed sessions, salted fingerprinting, and deferred writes
Database-backed sessions only (file driver removed). Salted SHA-256 fingerprint that an attacker can't reproduce without server-side salt. Deferred write: session lock released immediately after start(), in-memory delta flushed at end of request. flock-based regeneration lock for parallel requests.
-
One PHP codebase, three MySQL servers: per-domain database routing without code changes
How to route a single PHP codebase to different MySQL databases based on HTTP_HOST — without code changes in any model or controller. The full implementation: ConnectionRouter, strict host validation, TLS to remote DBs, and CLI --connection flag.