Skip to content

ORM autoMap produces broken fieldMapping for uppercase (Firebird) columns #102

@justin-k-bruce

Description

@justin-k-bruce

Bug

When $autoMap = true, the ORM's snakeToCamel() conversion produces incorrect property names for uppercase column names, which is the default for Firebird (and also common in Oracle).

Example

Firebird returns columns like ACCOUNTNO, STORENAME, CREDITLIMIT. The snakeToCamel() method applies lcfirst() which lowercases only the first character:

ACCOUNTNO  → aCCOUNTNO
STORENAME  → sTORENAME
CREDITLIMIT → cREDITLIMIT

Resulting fieldMapping:

[fieldMapping] => Array
(
    [aCCOUNTNO] => ACCOUNTNO
    [sTORENAME] => STORENAME
)

These mapped property names (aCCOUNTNO) don't match any declared PHP properties on the model, so the mapping is useless.

Root Cause

snakeToCamel() on line 98 of ORM.php:

private static function snakeToCamel(string $name): string
{
    return lcfirst(str_replace('_', '', ucwords($name, '_')));
}

This assumes columns are snake_case (e.g., credit_limit). It doesn't handle:

  1. ALL-CAPS columns (ACCOUNTNO, CREDIT_LIMIT) — common in Firebird, Oracle
  2. Single-word uppercase (STORENAME) — no underscores to split on

Suggested Fix

snakeToCamel() should lowercase the input first before applying the conversion:

private static function snakeToCamel(string $name): string
{
    return lcfirst(str_replace('_', '', ucwords(strtolower($name), '_')));
}

This produces:

ACCOUNTNO     → accountno
STORENAME     → storename
CREDIT_LIMIT  → creditLimit
FIRST_NAME    → firstName

Similarly, camelToSnake() may need to handle the reverse — when writing back to Firebird, column names should be uppercased:

private static function camelToSnake(string $name): string
{
    return strtolower(preg_replace('/[A-Z]/', '_$0', lcfirst($name)));
}

This already lowercases, but the generated mapping value should match the DB's actual casing. A possible improvement would be to preserve the original column casing from the DB result rather than assuming lowercase.

Environment

  • PHP 8.4.8
  • Tina4 v3.10.49
  • Firebird 4.0 (returns all-uppercase column names by default)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions