$v) { if (strpos($v, 'schoolId=') !== FALSE) { $schoolId = substr($v, strlen('schoolId=')); } } $databases=$this->getDatabases($schoolId); foreach($databases as $db){ echo 'start database_name:' . $db['database_name'] . '...' . PHP_EOL; $con = $this->getDbCon($db); if (!$con) { echo ' database cannot connect' . PHP_EOL; continue; } try { self::syncTpl($con); echo 'database_name:'.$db['database_name'] . 'done' . PHP_EOL; } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; continue; } sleep(0.1); } echo 'end 处理完成' . PHP_EOL; exit; } /** * 库连接 * @param $db * @return bool|CDbConnection */ public function getDbCon($db) { 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; } /** * 获取学校数据库 * @return mixed */ private function getDatabases($schoolId) { if($schoolId) { $dbs = BusinessDatabase::model()->findAll('school_id=:sid', array(':sid' => $schoolId)); }else{ $dbs= BusinessDatabase::model()->findAll(); } return $dbs; } /** * 同步学科数据 * @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(); } } } } }