From 964248a10ec524b6a9f965e0cb24a0b42a5d3c15 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 25 Apr 2016 12:15:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Query=E7=B1=BB=E7=9A=84chunk?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index d50f0ab6..8f52ca94 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1254,21 +1254,27 @@ class Query */ public function chunk($count, $callback, $column = null) { - $column = $column ?: $this->connection->getTableInfo('', 'pk'); - $options = $this->getOptions(); + $column = $column ?: $this->connection->getTableInfo('', 'pk'); + $options = $this->getOptions(); + if (empty($options['table'])) { + $table = $this->connection->getTable(); + } $resultSet = $this->limit($count)->order($column, 'asc')->select(); while (!empty($resultSet)) { if (false === call_user_func($callback, $resultSet)) { return false; } - $end = end($resultSet); - $lastId = is_array($end) ? $end[$column] : $end->$column; - $resultSet = $this->options($options) + $end = end($resultSet); + $lastId = is_array($end) ? $end[$column] : $end->$column; + $this->options($options) ->limit($count) ->where($column, '>', $lastId) - ->order($column, 'asc') - ->select(); + ->order($column, 'asc'); + if (isset($table)) { + $this->table($table); + } + $resultSet = $this->select(); } return true; }