@@ -31,6 +31,7 @@ class DatabaseHandler
3131 private $ nestMerger ;
3232
3333 private $ transactionStack = 0 ;
34+ private $ chunkSize = null ;
3435
3536 public function __construct (DriverInterface $ driver )
3637 {
@@ -77,6 +78,11 @@ public function allowDeleteWithoutWhere($allow = true)
7778 $ this ->allowDeleteWithoutWhere = $ allow ;
7879 }
7980
81+ public function setChunkSize (?int $ chunkSize )
82+ {
83+ $ this ->chunkSize = $ chunkSize ;
84+ }
85+
8086 public function executeSelect (SelectQuery $ query ): SelectResult
8187 {
8288 $ query = QueryOptimizer::optimizeSelect ($ query );
@@ -236,11 +242,14 @@ public function executeUpsert(UpsertQuery $query): UpsertResult
236242 $ insertedId = null ;
237243 $ success = true ;
238244 if (!empty ($ inserts )) {
239- $ insertQuery = Query::insertInto ($ query ->getTable (), $ inserts , $ query ->getColumns ());
240- $ insertResult = $ this ->executeInsert ($ insertQuery );
241- $ numInserts += $ insertResult ->getNumRows ();
242- $ insertedId = $ insertResult ->getInsertedId ();
243- $ success &= $ insertResult ->success ();
245+ $ insertChunks = array_chunk ($ inserts , $ this ->chunkSize ?? count ($ inserts ));
246+ foreach ($ insertChunks as $ chunk ) {
247+ $ insertQuery = Query::insertInto ($ query ->getTable (), $ chunk , $ query ->getColumns ());
248+ $ insertResult = $ this ->executeInsert ($ insertQuery );
249+ $ insertedId = $ insertedId ?? $ insertResult ->getInsertedId ();
250+ $ numInserts += $ insertResult ->getNumRows ();
251+ $ success &= $ insertResult ->success ();
252+ }
244253 }
245254 if (!empty ($ updates ) && !empty ($ query ->getUpdateColumns ())) {
246255 foreach ($ updates as $ u ) {
0 commit comments