Skip to content

Fix batchInsert() crash on array values for JSON columns#20768

Open
WarLikeLaux wants to merge 3 commits intoyiisoft:masterfrom
WarLikeLaux:fix-batch-insert-json-array
Open

Fix batchInsert() crash on array values for JSON columns#20768
WarLikeLaux wants to merge 3 commits intoyiisoft:masterfrom
WarLikeLaux:fix-batch-insert-json-array

Conversation

@WarLikeLaux
Copy link
Contributor

Q A
Is bugfix?
New feature?
Breaks BC?
Fixed issues #20683

What does this PR do?

Fixes batchInsert() crash ("Array to string conversion") when inserting PHP arrays into JSON columns. Alternative to #20684.

Problem

batchInsert() wraps arrays in JsonExpression via dbTypecast() only when table schema is found. When schema is unavailable (table name with {{%}} notation, missing table, etc.), raw arrays pass through all type checks and hit implode(), causing a PHP warning.

Fix

  1. Added base JsonExpressionBuilder in yii\db - default builder for backends without a vendor-specific one (SQLite, MSSQL, Oracle, CUBRID). MySQL and PostgreSQL keep their own.
  2. Registered it in QueryBuilder::defaultExpressionBuilders().
  3. Added is_array() fallback in batchInsert() across 3 QueryBuilder implementations (base, pgsql, oci) - wraps raw arrays in JsonExpression and processes via buildExpression().

Uses parameter binding (:qp0 placeholders) instead of inline json_encode(), which prevents SQL injection.

Tests

QueryBuilderTest (2) - array value with param binding, JsonExpression with Query subquery

@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.58%. Comparing base (379b296) to head (5e54dc3).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #20768      +/-   ##
============================================
+ Coverage     78.56%   78.58%   +0.01%     
- Complexity    11568    11573       +5     
============================================
  Files           376      377       +1     
  Lines         30750    30763      +13     
============================================
+ Hits          24160    24174      +14     
+ Misses         6590     6589       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@WarLikeLaux WarLikeLaux marked this pull request as ready for review March 4, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant