getSchools(); foreach ($schools as $school) { echo 'Current school_id:' . $school['school_id'] . PHP_EOL; $con = $this->getSchoolDbCon($school['school_id']); if (!$con) { echo 'school database cannot connect' . PHP_EOL; continue; } try{ $con->createCommand("UPDATE english_week SET week_group_id=week_id WHERE week_group_id=0;")->execute(); }catch (\Exception $e){ echo $e->getMessage() . PHP_EOL; continue; } echo 'School:' . $school['school_id'] . ' is OK!' . PHP_EOL; sleep(0.5); } echo 'end 更新晨读词汇历史数据批次id脚本处理完成' . PHP_EOL; exit; } /** * 获取所有正常可用的学校 * @return mixed */ private function getSchools() { $db = Yii::app()->businessDb; $sql = "SELECT school_id FROM `school` WHERE `status`=0"; $schools = $db->createCommand($sql)->queryAll(); $db->close(); return $schools; } /** * 学校库连接 * @param $schoolId * @return bool|CDbConnection */ public function getSchoolDbCon($schoolId){ $db = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId)); if(empty($db)){ return false; } $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name; $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password); $my_connection->emulatePrepare = true; $my_connection->enableProfiling = true; $my_connection->enableParamLogging = true; $myDbDsn = null; return $my_connection; } }