/** * 获取记录集 * @param \Sooh\DB\Interfaces\All $db * @param string $tb */ public function kvoGetRecordsStd($db, $tb) { $rs = $db->getRecords($tb, $this->fields, $this->where, $this->sortGroup, $this->pager->page_size, $this->pager->rsFrom()); foreach ($rs as $r) { $this->records[] = $r; } }
/** * 分页获取记录 * * 分表后,查询结果分页很复杂,提供这个函数解决部分情况(需要唯一索引,且最多2个字段): * * @param \Sooh\DB\Pager $pager * @param array $whereOrLastpage array-of-where OR string-of-lastPage * @param string $fields * @return array [lastPage=>'xxxx', records=array()] */ public static function loopGetRecords($pager, $whereOrLastpage = null, $fields = '*') { $obj = self::getCopy(null); $tool = new KVObjV2Loop(); $tool->sort_field_type = $obj->sortDefineForPage; $tool->pager = $pager; $tool->fields = $fields; $targetPage = $tool->pager->pageid(); $pager->init($pager->total, 1); $db = $obj->db(); $pageStep = $tool->kvoPagePrepare($targetPage, $whereOrLastpage, $db); while ($pageStep > 0) { $tool->records = []; $obj->loop([$tool, 'kvoGetRecordsStd']); // echo "\n----------\n"; // echo implode("\n",\Sooh\DB\Broker::lastCmd(false)); // echo "\n----------\n"; // foreach($tool->records as $r){ // echo json_encode($r)."\n"; // } // echo "---------\n"; $lastPage = $tool->kvoAfterOnePageLoaded($targetPage - $pageStep + 1); // echo "new lastPage: ".json_encode($lastPage)."\n"; // echo "---------\n"; $tool->kvoPagePrepare($targetPage, $lastPage, $db); $pageStep--; } $pager->init($pager->total, $targetPage); return ['lastPage' => $lastPage, 'records' => $tool->records]; }