1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- Yii::import('application.models.*');
- /**
- * 处理晨读词汇历史批次id数据脚本(一次性)
- */
- class EnglishWeekBatchCommand extends CConsoleCommand {
- public function init() {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- public function actionIndex($YII_ENV='development')
- {
- echo 'start 批量更新晨读词汇历史数据批次id脚本开始.....'.PHP_EOL;
- $schools = $this->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;
- }
- }
|