EnglishWeekBatchCommand.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. Yii::import('application.models.*');
  3. /**
  4. * 处理晨读词汇历史批次id数据脚本(一次性)
  5. */
  6. class EnglishWeekBatchCommand extends CConsoleCommand {
  7. public function init() {
  8. parent::init();
  9. @ini_set('memory_limit', '1024M');
  10. set_time_limit(0);
  11. }
  12. public function actionIndex($YII_ENV='development')
  13. {
  14. echo 'start 批量更新晨读词汇历史数据批次id脚本开始.....'.PHP_EOL;
  15. $schools = $this->getSchools();
  16. foreach ($schools as $school) {
  17. echo 'Current school_id:' . $school['school_id'] . PHP_EOL;
  18. $con = $this->getSchoolDbCon($school['school_id']);
  19. if (!$con) {
  20. echo 'school database cannot connect' . PHP_EOL;
  21. continue;
  22. }
  23. try{
  24. $con->createCommand("UPDATE english_week SET week_group_id=week_id WHERE week_group_id=0;")->execute();
  25. }catch (\Exception $e){
  26. echo $e->getMessage() . PHP_EOL;
  27. continue;
  28. }
  29. echo 'School:' . $school['school_id'] . ' is OK!' . PHP_EOL;
  30. sleep(0.5);
  31. }
  32. echo 'end 更新晨读词汇历史数据批次id脚本处理完成' . PHP_EOL;
  33. exit;
  34. }
  35. /**
  36. * 获取所有正常可用的学校
  37. * @return mixed
  38. */
  39. private function getSchools()
  40. {
  41. $db = Yii::app()->businessDb;
  42. $sql = "SELECT school_id FROM `school` WHERE `status`=0";
  43. $schools = $db->createCommand($sql)->queryAll();
  44. $db->close();
  45. return $schools;
  46. }
  47. /**
  48. * 学校库连接
  49. * @param $schoolId
  50. * @return bool|CDbConnection
  51. */
  52. public function getSchoolDbCon($schoolId){
  53. $db = BusinessDatabase::model()->find('school_id=:sid',array(':sid'=>$schoolId));
  54. if(empty($db)){
  55. return false;
  56. }
  57. $myDbDsn = 'mysql:host='.$db->database_host.';dbname='.$db->database_name;
  58. $my_connection = new CDbConnection($myDbDsn,$db->database_user,$db->database_password);
  59. $my_connection->emulatePrepare = true;
  60. $my_connection->enableProfiling = true;
  61. $my_connection->enableParamLogging = true;
  62. $myDbDsn = null;
  63. return $my_connection;
  64. }
  65. }