SubSynTableModel = M("syn_table","sub_",SUBSITE_DB);//指定子库 } public function run(){ $synList = $this->SubSynTableModel->field("id,table_name,table_prefix,check_sum")->select(); foreach ($synList as $k => $v) { $this->doSyn($v); } } public function synOneTable($table_prefix,$tab_name) { $synInfo = $this->SubSynTableModel->where("table_name = '{$tab_name}' and table_prefix = '{$table_prefix}'")->field("id,table_name,table_prefix,check_sum")->find(); $this->doSyn($synInfo); } protected function doSyn($dbarr) { //获取最新check_sum $table_prefix = $dbarr['table_prefix']; $table_name = $dbarr['table_name']; $res = M()->query(self::CHECK_SUM_SQL.$table_prefix.$table_name); $now = time(); $check_sum = $res[0]['checksum']; if($check_sum !== $dbarr['check_sum']){ $this->clearTable($table_prefix,$table_name); $this->synTable($table_prefix,$table_name); $dbarr['syn_time'] = $now; $dbarr['check_sum'] = $check_sum; } $dbarr['update_time'] = $now; $this->SubSynTableModel->save($dbarr); echo $table_prefix.$table_name." success".PHP_EOL; } protected function clearTable($table_prefix,$table_name) { return SM()->query(self::TRUNCATE_SQL.$table_prefix.$table_name); } protected function synTable($table_prefix,$table_name) { $count = M($table_name,$table_prefix)->count(); if(empty($count)) return true; $pageCount = ceil($count/self::LIMIT); for ($i=1; $i <= $pageCount; $i++) { $tres = M($table_name,$table_prefix)->page($i,self::LIMIT)->select(); $addRes = M($table_name,$table_prefix,SUBSITE_DB)->addAll($tres); echo "[{$i}/{$pageCount}] {$addRes}".PHP_EOL; } return true; } }