$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::syncSetting($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; } /** * 获取所有正常可用的学校 * @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; } public function syncSetting($con){ $sql = "select product_type,custom_config from product_setting where product_type = 6 order by ps_id "; $templates = $con->createCommand($sql)->queryRow(); if($templates){ $templates = json_decode($templates['custom_config'],true); if(!isset($templates['setting']['productName'])){ $templates['setting']['productName'] = '个性化学习宝'; } if(!isset($templates['setting']['oldPaperAnswer'])){ $templates['setting']['oldPaperAnswer'] = 1; } //$templates = json_encode($templates,JSON_UNESCAPED_UNICODE); $templates = $this->myjson($templates); $sql = "update product_setting set custom_config = '$templates' where product_type=6 "; $templates = $con->createCommand($sql)->execute(); echo sprintf("同步完成").PHP_EOL; } } public function myjson($code) { $code = json_encode(self::urlencodeAry($code)); return urldecode($code); } public static function urlencodeAry($data) { if(is_array($data)) { foreach($data as $key=>$val) { $data[$key] = static::urlencodeAry($val); } return $data; } else { return urlencode($data); } } }