$v) { if (strpos($v, 'schoolId=') !== FALSE) { $schoolId = substr($v, strlen('schoolId=')); } } $schools = isset($schoolId) ? array(0=>array('school_id'=> $schoolId)) : $this->getSchools(); foreach ($schools as $school) { echo 'start school_id:' . $school['school_id'] . '...' . PHP_EOL; $con = $this->getSchoolDbCon($school['school_id']); if (!$con) { echo 'school database cannot connect' . PHP_EOL; continue; } try { self::syncTpl($con); echo 'school_id:'.$school['school_id'] . 'done' . PHP_EOL; } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; continue; } sleep(0.1); } echo 'end 处理完成' . PHP_EOL; exit; } /** * 获取所有正常可用的学校 * @return mixed */ private function getSchools() { $db = Yii::app()->businessDb; $sql = "SELECT school_id,school_name 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; } /** * 同步学科数据 * @param $con object 数据库连接 */ private function syncTpl($con) { $spsIds=array(1,2,3,4); foreach($spsIds as $spsId){ $sql="select * from subject_product_setting where sps_id={$spsId}"; $subjectProductStting = $con->createCommand($sql)->queryRow(); $time=time(); if(!$subjectProductStting){ $insertSql=''; if($spsId==1){ $insertSql="INSERT INTO `subject_product_setting` VALUES (1, 3, '数学', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)"; }elseif($spsId==2){ $insertSql="INSERT INTO `subject_product_setting` VALUES (2, 12, '物理', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)"; }elseif($spsId==3){ $insertSql="INSERT INTO `subject_product_setting` VALUES (3, 8, '英语', '{\"new_version_size\":false}', {$time}, 0)"; }elseif($spsId==4){ $insertSql="INSERT INTO `subject_product_setting` VALUES (4, 0, '其他学科', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)"; } if($insertSql){ echo $insertSql.PHP_EOL; $con->createCommand($insertSql)->execute(); } } } } }