fix(database-mysql): JSON-encode array bindings before passing to PDO#82
Merged
Merged
Conversation
PDO silently casts PHP arrays to the string "Array" when bound as query parameters. For json columns this either fails the JSON parse or stores the literal string "Array" depending on column type and SQL mode. Mirrors the database-pgsql fix from #72: pre-process bindings via a prepareBindings() helper that JSON-encodes any array values with JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE. JsonException is caught and converted to a domain ConnectionException::invalidArrayBinding() that names the offending parameter. Closes #81 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #81
Summary
PDO silently casts PHP arrays to the string
"Array"when bound as query parameters. For JSON-typed columns this either fails the JSON parse or stores the literal string"Array"depending on column type and SQL mode — the same root cause as #71 / #72, but in the MySQL driver.Fix
Mirrors the
database-pgsqlapproach:prepareBindings()helper toMySqlConnectionthat walks the bindings array and JSON-encodes any array values withJSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE.JsonExceptionis caught and converted to a domainConnectionException::invalidArrayBinding($parameter, $previous)with a clear message identifying the offending parameter.MySqlConnection::query()andMySqlConnection::execute()now route throughprepareBindings()before calling$statement->execute().Tests
it JSON-encodes array bindings instead of casting them to the string "Array"— happy path, end-to-end via an SQLite-backedMySqlConnection.it throws ConnectionException when an array binding is not JSON-encodable— failure path usingNANas an array value.Full suite green: 5147 passing.
Test plan
./vendor/bin/pest packages/database-mysql/tests/ --parallel./vendor/bin/phpcsclean on touched files./vendor/bin/php-cs-fixer fix --dry-runclean on touched filescomposer test🤖 Generated with Claude Code