Skip to content

feat: support BackedEnum values in database binds#10256

Open
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/query-builder-backed-enums
Open

feat: support BackedEnum values in database binds#10256
memleakd wants to merge 1 commit into
codeigniter4:4.8from
memleakd:feat/query-builder-backed-enums

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

Description

This PR adds support for PHP BackedEnum values in database escaping, query bindings, and escaped Query Builder values.

It was inspired by @maniaba's forum post and #10223.

For apps that use native PHP enums for domain values, this makes database code a bit nicer to write:

$db->table('users')
    ->where('status', UserStatus::Active)
    ->get();

CodeIgniter will use the enum's backing value when escaping it, so string-backed enums are treated like strings and int-backed enums are treated like integers.

This avoids repeating ->value at every query call site, while keeping raw SQL and escape-disabled values in the caller's control.

The implementation is intentionally small: enum cases are unwrapped during database escaping, so regular query bindings and escaped Query Builder values use the same existing path.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- Allow database escaping to use BackedEnum backing values
- Support BackedEnum values in query bindings and Query Builder binds
- Add focused tests and user guide examples

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 31, 2026
Copy link
Copy Markdown
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR look good to me.


$expectedSQL = 'SELECT * FROM "jobs" WHERE "status" IN (\'active\',\'inactive\')';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While reviewing this (specifically the use of str_replace for query formatting), a broader architectural thought came to my mind: Why don't we have a dedicated assertSqlEquals method?

Currently, across many test files, we are writing boilerplate code like str_replace("\n", ' ', ...) to normalize SQL strings before comparing them. We could easily add an assertSqlEquals() method directly into use CodeIgniter\Test\DatabaseTestTrait;.

Something like this:

public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
    $normalize = fn(string $sql) => trim(strtolower(preg_replace('/\s+/', ' ', $sql)));
    $this->assertSame($normalize($expected), $normalize($actual), $message);
}

Let's see what the team thinks about this, though I believe it should be followed up separately in its own PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

Yeah, I like that idea as a follow-up. I'd keep this PR focused, but a small test helper for this repeated SQL formatting pattern would definitely be worth exploring separately.

Only thing I'd be careful with is making the normalization too clever, so it doesn't accidentally hide meaningful SQL differences.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this normalization:

trim(strtolower(preg_replace('/\s+/', ' ', $sql)))

That would transform our SQL way too much silently.

Personally, I would stick to what we use currently, so:

public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
    $this->assertSame(
        $expected,
        str_replace("\n", ' ', $actual),
        $message,
    );
}

would make sense to me. But I would place it in the CIUnitTestCase because many builder unit tests don't use/need DatabaseTestTrait.

Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like native prepared statements are not supported.

$query = $db->prepare(...);
$query->execute(StatusEnum::ACTIVE);

$expectedSQL = 'INSERT INTO "jobs" ("id", "status") VALUES (1, \'active\')';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledInsert()));
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to make sure objects are also supported.


$expectedSQL = 'SELECT * FROM "jobs" WHERE "status" IN (\'active\',\'inactive\')';

$this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect()));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this normalization:

trim(strtolower(preg_replace('/\s+/', ' ', $sql)))

That would transform our SQL way too much silently.

Personally, I would stick to what we use currently, so:

public function assertSqlEquals(string $expected, string $actual, string $message = ''): void
{
    $this->assertSame(
        $expected,
        str_replace("\n", ' ', $actual),
        $message,
    );
}

would make sense to me. But I would place it in the CIUnitTestCase because many builder unit tests don't use/need DatabaseTestTrait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants