@@ -2894,92 +2894,6 @@ public function batchUpdateByIds(array $values, string $primary = 'id')
28942894 return $ affectedRows ;
28952895 }
28962896
2897- /**
2898- * Batch Update Or Insert, UpdateOrInsert operating suggest add unique index
2899- *
2900- * @param array $items origin item
2901- * @param array $baseWhere only support [key=>value] where
2902- * @param array $whereKeys exists data where item
2903- * @param array $updateKeys update item key
2904- * @param array $incrKeys increment item key
2905- * @param string $primary table primary
2906- *
2907- * @return bool
2908- * @throws ContainerException
2909- * @throws DbException
2910- * @throws ReflectionException
2911- */
2912- public function batchUpdateOrInsert (
2913- array $ items ,
2914- array $ baseWhere ,
2915- array $ whereKeys = [],
2916- array $ updateKeys = [],
2917- array $ incrKeys = [],
2918- string $ primary = 'id '
2919- ): bool {
2920- $ wheres = [];
2921- foreach ($ items as $ k => $ v ) {
2922- foreach ($ whereKeys as $ whereKey ) {
2923- $ value = $ v [$ whereKey ];
2924- $ wheres [$ whereKey ][$ value ] = $ value ;
2925- }
2926-
2927- if ($ baseWhere ) {
2928- $ items [$ k ] = array_merge ($ baseWhere , $ v );
2929- }
2930- }
2931-
2932- $ existMaps = [];
2933-
2934- $ searchWhere = $ wheres ;
2935- if ($ baseWhere ) {
2936- $ searchWhere = array_merge ($ baseWhere , $ wheres );
2937- }
2938-
2939- $ exitsList = $ this ->select ($ primary , ...$ whereKeys )->where ($ searchWhere )->get ();
2940- $ uniqueKeys = array_keys ($ searchWhere );
2941- foreach ($ exitsList as $ record ) {
2942- $ mergeData = $ record ;
2943- if ($ baseWhere ) {
2944- $ mergeData = array_merge ($ baseWhere , $ record );
2945- }
2946-
2947- $ uniqueId = ArrayHelper::toString (
2948- ArrayHelper::only ($ mergeData , $ uniqueKeys )
2949- );
2950- $ existMaps [$ uniqueId ] = $ record [$ primary ];
2951- }
2952-
2953- $ addItems = $ updateItems = [];
2954- foreach ($ items as $ item ) {
2955- $ uniqueId = ArrayHelper::toString (ArrayHelper::only ($ item , $ uniqueKeys ));
2956-
2957- if (isset ($ existMaps [$ uniqueId ])) {
2958- $ updateItem = array_merge (
2959- ArrayHelper::only ($ item , $ updateKeys ),
2960- $ this ->warpCounters (ArrayHelper::only ($ item , $ incrKeys ))
2961- );
2962- $ updateItems [] = $ updateItem ;
2963-
2964- continue ;
2965- }
2966-
2967- $ addItems [] = $ item ;
2968- }
2969-
2970- $ updateRes = $ addRes = true ;
2971-
2972- if ($ updateItems ) {
2973- $ updateRes = $ this ->batchUpdateByIds ($ updateItems );
2974- }
2975-
2976- if ($ addItems ) {
2977- $ addRes = $ this ->insert ($ addItems );
2978- }
2979-
2980- return $ updateRes && $ addRes ;
2981- }
2982-
29832897 /**
29842898 * Insert a new record and get the value of the primary key.
29852899 *
0 commit comments