@@ -324,4 +324,162 @@ public function testUpdateBatchConstraintsRawSqlAndAlias()
324
324
'country ' => 'UK ' ,
325
325
]);
326
326
}
327
+
328
+ public function testUpdateBatchUpdateFieldsAndAlias ()
329
+ {
330
+ if ($ this ->db ->DBDriver === 'SQLite3 ' && ! (version_compare ($ this ->db ->getVersion (), '3.33.0 ' ) >= 0 )) {
331
+ $ this ->markTestSkipped ('Only SQLite 3.33 and newer can complete this test. ' );
332
+ }
333
+
334
+ $ data = [
335
+ [
336
+
337
+ 'name ' => 'Derek Jones Does Not Change ' ,
338
+ 'country ' => 'Greece ' ,
339
+ ],
340
+ [
341
+
342
+
343
+ 'name ' => 'Ahmadinejad No change ' ,
344
+ 'country ' => 'Greece ' ,
345
+ ],
346
+ ];
347
+
348
+ $ rawSql = new RawSql ('CURRENT_TIMESTAMP ' );
349
+
350
+ $ updateFields = ['country ' , 'updated_at ' => $ rawSql ];
351
+
352
+ $ this ->db ->table ('user ' )->updateFields ($ updateFields )->onConstraint ('email ' )->updateBatch ($ data );
353
+
354
+ // check to see if update_at was updated
355
+ $ result = $ this ->db ->table ('user ' )
356
+
357
+ ->get ()
358
+ ->getResultArray ();
359
+
360
+ foreach ($ result as $ row ) {
361
+ $ this ->assertNotNull ($ row ['updated_at ' ]);
362
+ }
363
+
364
+ // only country and update_at should have changed
365
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Derek Jones ' , 'country ' => 'Greece ' ]);
366
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Ahmadinejad ' , 'country ' => 'Greece ' ]);
367
+
368
+ // Original dataset from seeder
369
+ $ data = [
370
+ [
371
+ 'name ' => 'Derek Should Change ' ,
372
+
373
+ 'country ' => 'Greece ' , // will update
374
+ ],
375
+ [
376
+ 'name ' => 'Ahmadinejad ' , // did't change above and will not change
377
+
378
+ 'country ' => 'Iran ' , // will not update
379
+ ],
380
+ [
381
+ 'name ' => 'Should Not Change ' ,
382
+
383
+ 'country ' => 'Greece ' , // will not update
384
+ ],
385
+ [
386
+ 'name ' => 'Should Change ' ,
387
+
388
+ 'country ' => 'UK ' , // will update
389
+ ],
390
+ ];
391
+
392
+ $ updateFields = ['name ' , 'updated_at ' => new RawSql ('NULL ' )];
393
+
394
+ $ esc = $ this ->db ->escapeChar ;
395
+
396
+ // contraint is email and if the updated country = the source country
397
+ // setting alias allows us to reference it in RawSql
398
+ $ this ->db ->table ('user ' )
399
+ ->updateFields ($ updateFields )
400
+ ->onConstraint (['email ' , new RawSql ("{$ esc }db_user {$ esc }. {$ esc }country {$ esc } = {$ esc }_update {$ esc }. {$ esc }country {$ esc }" )])
401
+ ->setAlias ('_update ' )
402
+ ->updateBatch ($ data );
403
+
404
+ $ result = $ this ->db ->table ('user ' )->get ()->getResultArray ();
405
+
406
+ foreach ($ result as $ row ) {
407
+ if (
$ row[
'email ' ] ===
'[email protected] ' ) {
408
+ $ this ->assertNotNull ($ row ['updated_at ' ]);
409
+ } else {
410
+ $ this ->assertNull ($ row ['updated_at ' ]);
411
+ }
412
+ }
413
+
414
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Derek Should Change ' , 'country ' => 'Greece ' ]);
415
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Ahmadinejad ' , 'country ' => 'Greece ' ]);
416
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Richard A Causey ' , 'country ' => 'US ' ]);
417
+ $ this ->seeInDatabase ('user ' , ['name ' => 'Should Change ' , 'country ' => 'UK ' ]);
418
+ }
419
+
420
+ public function testUpdateBatchWithoutOnConstraint ()
421
+ {
422
+ if ($ this ->db ->DBDriver === 'SQLite3 ' && ! (version_compare ($ this ->db ->getVersion (), '3.33.0 ' ) >= 0 )) {
423
+ $ this ->markTestSkipped ('Only SQLite 3.33 and newer can complete this test. ' );
424
+ }
425
+
426
+ $ data = [
427
+ [
428
+ 'name ' => 'Derek Nothing ' , // won't update
429
+
430
+ 'country ' => 'Canada ' ,
431
+ ],
432
+ [
433
+ 'name ' => 'Ahmadinejad ' ,
434
+
435
+ 'country ' => 'Canada ' ,
436
+ ],
437
+ [
438
+ 'name ' => 'Richard A Causey ' ,
439
+
440
+ 'country ' => 'Canada ' ,
441
+ ],
442
+ [
443
+ 'name ' => 'Chris Martin ' ,
444
+
445
+ 'country ' => 'Canada ' ,
446
+ ],
447
+ ];
448
+
449
+ $ this ->db ->table ('user ' )->updateBatch ($ data , 'email, name ' , 2 );
450
+
451
+ $ result = $ this ->db ->table ('user ' )->get ()->getResultArray ();
452
+
453
+ foreach ($ result as $ row ) {
454
+ if (
$ row[
'email ' ] ===
'[email protected] ' ) {
455
+ $ this ->assertSame ('US ' , $ row ['country ' ]);
456
+ } else {
457
+ $ this ->assertSame ('Canada ' , $ row ['country ' ]);
458
+ }
459
+ }
460
+ }
461
+
462
+ public function testRawSqlConstraint ()
463
+ {
464
+ if ($ this ->db ->DBDriver === 'SQLite3 ' && ! (version_compare ($ this ->db ->getVersion (), '3.33.0 ' ) >= 0 )) {
465
+ $ this ->markTestSkipped ('Only SQLite 3.33 and newer can complete this test. ' );
466
+ }
467
+
468
+ $ data = [
469
+ [
470
+ 'name ' => 'Derek Jones ' ,
471
+
472
+ 'country ' => 'Germany ' ,
473
+ ],
474
+ ];
475
+
476
+ $ builder = $ this ->db ->table ('user ' );
477
+
478
+ $ builder ->setData ($ data , true , 'db_myalias ' )
479
+ ->updateFields ('name, country ' )
480
+ ->onConstraint (new RawSql ($ this ->db ->protectIdentifiers ('user.email ' ) . ' = ' . $ this ->db ->protectIdentifiers ('myalias.email ' )))
481
+ ->updateBatch ();
482
+
483
+ $ this ->
seeInDatabase (
'user ' , [
'email ' =>
'[email protected] ' ,
'country ' =>
'Germany ' ]);
484
+ }
327
485
}
0 commit comments