数据查询
public function selectCursorWaitingIncentReceiptRecords()
{
$arr_sql = array();
$arr_sql[] = ' SELECT';
$arr_sql[] = ' receipt_id,';
$arr_sql[] = ' group_id,';
$arr_sql[] = ' reason_id,';
$arr_sql[] = ' tx_key,';
$arr_sql[] = ' tx_sub_key,';
$arr_sql[] = ' easy_id,';
$arr_sql[] = ' data_kbn,';
$arr_sql[] = ' point,';
$arr_sql[] = ' tx_date,';
$arr_sql[] = ' item_name,';
$arr_sql[] = ' item_url,';
$arr_sql[] = ' item_price,';
$arr_sql[] = ' shop_id,';
$arr_sql[] = ' shop_name,';
$arr_sql[] = ' shop_url,';
$arr_sql[] = ' receipt_datetime,';
$arr_sql[] = ' status,';
$arr_sql[] = ' result';
$arr_sql[] = ' FROM rcash_incent_receipt_tbl ';
$arr_sql[] = ' WHERE ';
$arr_sql[] = ' status = 10';
$sql = implode(' ', array_map('trim', $arr_sql));
$stmt = $this->prepare($sql);
return $this->execute($stmt);
}
查询数据的使用
public function executeBulk()
{
$dbi = new rcIncentiveDbiIncentReceiptTbl();
$waiting_records = $dbi->selectCursorWaitingIncentReceiptRecords();
$is_first = true;
while (($waiting_record = $dbi->fetchModel($waiting_records))) {
if ($is_first) {
rcBlContext::addMessage(rcMsgKeys::RC_I_TARGET_DATA);
$is_first = false;
}
rcBlContext::addMessage(rcMsgKeys::RC_I_LOG_START_WORK, 'receipt_id:' . $waiting_record->getReceiptId());
$this->execute($waiting_record);
}
return;
}
想要对预处理查询后的pdo对象,即$waiting_records做排序,排序之后的代码如下
public function executeBulk()
{
$dbi = new rcIncentiveDbiIncentReceiptTbl();
$waiting_records = $dbi->selectCursorWaitingIncentReceiptRecords();
$is_first = true;
$records = [];
while (($waiting_record = $dbi->fetchModel($waiting_records))) {
array_push($records, $waiting_record);
}
usort($records, function($a,$b){
if($a->getTxKey() == $b->getTxKey()){
return 0;
}
return ($a->getTxKey() < $b->getTxKey()) ? -1 : 1;
});
foreach ($records as $waiting_record) {
if ($is_first) {
rcBlContext::addMessage(rcMsgKeys::RC_I_TARGET_DATA);
$is_first = false;
}
rcBlContext::addMessage(rcMsgKeys::RC_I_LOG_START_WORK, 'receipt_id:' . $waiting_record->getReceiptId());
$this->execute($waiting_record);
}
return;
}
总结:pdo对象不是一个数组,但是里面又是数组的结构。开始一直在纠结怎么可以把一个不是数组的对象做数组排序呢。开始把pdo对象转换成了数组,可是后续需要的时model,不是数组。所以纠结了很久一直不知道怎么处理。 原代码是把pdo对象直接遍历出model来用,最终的解决方案是:把model都先取出来放在一个数组records里,然后对这个数组按照tx_key做排序,排序完之后再对这个数组做循环取出model。
排序方法:usort自定义排序
|