@@ -582,6 +582,7 @@ abstract public function countAllResults(bool $reset = true, bool $test = false)
582582 * @return void
583583 *
584584 * @throws DataException
585+ * @throws InvalidArgumentException if $size is not a positive integer
585586 */
586587 abstract public function chunk (int $ size , Closure $ userFunc );
587588
@@ -1099,15 +1100,35 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10991100 {
11001101 if (is_array ($ set )) {
11011102 foreach ($ set as &$ row ) {
1103+ // Save the index value from the original row because
1104+ // transformDataToArray() may strip it when updateOnlyChanged
1105+ // is true.
1106+ $ updateIndex = null ;
1107+
1108+ if ($ this ->updateOnlyChanged ) {
1109+ if (is_array ($ row )) {
1110+ $ updateIndex = $ row [$ index ] ?? null ;
1111+ } elseif ($ row instanceof Entity) {
1112+ $ updateIndex = $ row ->toRawArray ()[$ index ] ?? null ;
1113+ } elseif (is_object ($ row )) {
1114+ $ updateIndex = $ row ->{$ index } ?? null ;
1115+ }
1116+ }
1117+
11021118 $ row = $ this ->transformDataToArray ($ row , 'update ' );
11031119
11041120 // Validate data before saving.
11051121 if (! $ this ->skipValidation && ! $ this ->validate ($ row )) {
11061122 return false ;
11071123 }
11081124
1109- // Save updateIndex for later
1110- $ updateIndex = $ row [$ index ] ?? null ;
1125+ // When updateOnlyChanged is true, restore the pre-extracted
1126+ // index into $row. Otherwise read it from the transformed row.
1127+ if ($ updateIndex !== null ) {
1128+ $ row [$ index ] = $ updateIndex ;
1129+ } else {
1130+ $ updateIndex = $ row [$ index ] ?? null ;
1131+ }
11111132
11121133 if ($ updateIndex === null ) {
11131134 throw new InvalidArgumentException (
0 commit comments