@@ -540,6 +540,7 @@ public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition
540540 $ columnSchemas = $ tableSchema !== null ? $ tableSchema ->columns : [];
541541
542542 $ keys = $ this ->resolveKeys ($ table , $ keys );
543+ $ rows = $ this ->prepareRows ($ rows );
543544 $ rows = $ this ->resolveColumnNames ($ rows , $ columns );
544545
545546 $ isSingleKey = count ($ keys ) === 1 ;
@@ -553,13 +554,6 @@ public function batchUpdate($table, $rows, $columns = [], $keys = [], $condition
553554 $ rowKeyData = [];
554555
555556 foreach ($ rows as $ row ) {
556- if ($ row instanceof \Traversable) {
557- $ row = iterator_to_array ($ row );
558- }
559- if (!is_array ($ row )) {
560- throw new InvalidArgumentException ('Each batch update row must be an array. ' );
561- }
562-
563557 $ keyData = [];
564558 $ keyHashParts = [];
565559 foreach ($ keys as $ keyCol ) {
@@ -705,15 +699,38 @@ protected function resolveKeys($table, $keys)
705699 );
706700 }
707701
702+ /**
703+ * Converts Traversable objects to arrays and validates that all rows are arrays.
704+ *
705+ * @param array|\Generator $rows the rows to prepare.
706+ * @return array[] prepared rows.
707+ * @since 2.0.55
708+ */
709+ protected function prepareRows ($ rows )
710+ {
711+ $ prepared = [];
712+ foreach ($ rows as $ row ) {
713+ if ($ row instanceof \Traversable) {
714+ $ row = iterator_to_array ($ row );
715+ }
716+ if (!is_array ($ row )) {
717+ throw new InvalidArgumentException ('Each batch update row must be an array. ' );
718+ }
719+ $ prepared [] = $ row ;
720+ }
721+
722+ return $ prepared ;
723+ }
724+
708725 /**
709726 * Resolves batch update row column names.
710727 *
711728 * When `$columns` is specified, converts indexed arrays to associative arrays
712729 * using column names as keys (same convention as [[batchInsert()]]).
713730 *
714- * @param array|\Generator $rows the rows to resolve.
731+ * @param array[] $rows the rows to resolve (already prepared by [[prepareRows()]]) .
715732 * @param array $columns list of column names.
716- * @return array resolved rows as associative arrays.
733+ * @return array[] resolved rows as associative arrays.
717734 * @since 2.0.55
718735 */
719736 protected function resolveColumnNames ($ rows , $ columns )
@@ -725,12 +742,6 @@ protected function resolveColumnNames($rows, $columns)
725742 $ resolvedRows = [];
726743 $ columnCount = count ($ columns );
727744 foreach ($ rows as $ row ) {
728- if ($ row instanceof \Traversable) {
729- $ row = iterator_to_array ($ row );
730- }
731- if (!is_array ($ row )) {
732- throw new InvalidArgumentException ('Each batch update row must be an array. ' );
733- }
734745 if (count ($ row ) !== $ columnCount ) {
735746 throw new InvalidArgumentException (
736747 'Each batch update row must have exactly ' . $ columnCount . ' values when $columns is specified. '
@@ -749,11 +760,10 @@ protected function resolveColumnNames($rows, $columns)
749760 *
750761 * @param string $table the table that data will be saved into.
751762 * @param array $columns the column data (name => value) to be saved into the table.
752- * @param array $params the binding parameters that will be modified by this method.
753763 * @return array normalized columns.
754764 * @since 2.0.55
755765 */
756- protected function normalizeTableRowData ($ table , $ columns, & $ params )
766+ protected function normalizeTableRowData ($ table , $ columns )
757767 {
758768 return $ columns ;
759769 }
@@ -767,30 +777,21 @@ protected function normalizeTableRowData($table, $columns, &$params)
767777 * @param string $table the table name.
768778 * @param array $rows the rows to normalize.
769779 * @param array $keys the key column names.
770- * @param array $params the binding parameters that will be modified by this method.
771780 * @return array normalized rows.
772781 * @since 2.0.55
773782 */
774- protected function normalizeBatchUpdateRows ($ table , $ rows , $ keys, & $ params )
783+ protected function normalizeBatchUpdateRows ($ table , $ rows , $ keys )
775784 {
776785 $ normalizedRows = [];
777786 foreach ($ rows as $ row ) {
778- if ($ row instanceof \Traversable) {
779- $ row = iterator_to_array ($ row );
780- }
781- if (!is_array ($ row )) {
782- $ normalizedRows [] = $ row ;
783- continue ;
784- }
785-
786787 $ keyValues = [];
787788 foreach ($ keys as $ keyCol ) {
788789 if (array_key_exists ($ keyCol , $ row )) {
789790 $ keyValues [$ keyCol ] = $ row [$ keyCol ];
790791 unset($ row [$ keyCol ]);
791792 }
792793 }
793- $ row = $ this ->normalizeTableRowData ($ table , $ row, $ params );
794+ $ row = $ this ->normalizeTableRowData ($ table , $ row );
794795 foreach ($ keyValues as $ keyCol => $ keyValue ) {
795796 $ row [$ keyCol ] = $ keyValue ;
796797 }
0 commit comments