ii2 batchInsert 대량 삽입

2561 단어 yii2
		foreach ($importData as $k => $data) {
                    // id 
                    $gameId = $data[0];
                    $gameName = MoxGame::getGameByGameid($gameId);
                    if ($gameId == 1) {// id 1 , 1
                        continue;
                    }
                    if (empty($gameName)) { // 
                        $fail++;
                        $failGameId .= $gameId . ',';
                        continue;
                    }
                    // 
                    if (!empty($data[3])) {
                        $data[3] = strtotime(gmdate('Y-m-d H:i', \PHPExcel_Shared_Date::ExcelToPHP($data[3])));
                    }
                    // 
                    $data[] = $type;  // 
                    $data[] = time();
                    $data[] = Yii::$app->user->identity->username;
					$newData[] = $data;
                    $newDataStr .= "('" . implode("','", $data) . "'),";

                    // 
                    $old = [];
                    $old[0] = $data[0];
                    $old[1] = $data[2];
                    $old[2] = $data[3];
                    $old[3] = $data[4];
                    $oldDataStr .= "('" . implode("','", $old) . "'),";
                }

                //1. 
                $col = ['gameid', 'server_id', 'server', 'start_time', 'sort', 'is_recommend', 'type', 'created_at', 'created_user'];
                $sql = Yii::$app->db->createCommand()->batchInsert('cy_server', $col, $newData)->getRawSql();
                debug(" sql:".$sql);
                $num = $db->createCommand()->batchInsert('cy_server', $col, $newData)->execute();
				
		//2. 
		$col = "`gameid`, `server_id`, `server`, `start_time`, `sort`, `is_recommend`, `type`, `created_at`, `created_user`";
                $newDataStr = trim($newDataStr, ',');
                $sql = "INSERT IGNORE INTO cy_server ($col) VALUES $newDataStr";
                $db->createCommand($sql)->execute();

                //3. 
                $col = "`gameid`, `sername`, `sertime`, `order`";
                $oldDataStr = trim($oldDataStr, ',');
                $sql = "REPLACE INTO cy_serverinfo ($col) VALUES $oldDataStr";
                $btboxDb->createCommand($sql)->execute();

좋은 웹페이지 즐겨찾기