From 6b98a9974cc418eec89cfbc2036c58b9b4e7a2d8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 19 Aug 2017 10:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84chunk?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E6=8E=92=E5=BA=8F=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 91499647..4cea36ea 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2566,9 +2566,10 @@ class Query * @param integer $count 每次处理的数据数量 * @param callable $callback 处理回调方法 * @param string $column 分批处理的字段名 + * @param string $order 排序规则 * @return boolean */ - public function chunk($count, $callback, $column = null) + public function chunk($count, $callback, $column = null, $order = 'asc') { $options = $this->getOptions(); if (isset($options['table'])) { @@ -2576,9 +2577,12 @@ class Query } else { $table = ''; } - $column = $column ?: $this->getPk($table); + $column = $column ?: $this->getPk($table); + if (is_array($column)) { + $column = $column[0]; + } $bind = $this->bind; - $resultSet = $this->limit($count)->order($column, 'asc')->select(); + $resultSet = $this->limit($count)->order($column, $order)->select(); if (strpos($column, '.')) { list($alias, $key) = explode('.', $column); } else { @@ -2597,8 +2601,8 @@ class Query $resultSet = $this->options($options) ->limit($count) ->bind($bind) - ->where($column, '>', $lastId) - ->order($column, 'asc') + ->where($column, 'asc' == $order ? '>' : '<', $lastId) + ->order($column, $order) ->select(); if ($resultSet instanceof Collection) { $resultSet = $resultSet->all();