diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9a274fd5..06412895 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -2605,47 +2605,51 @@ class Query public function chunk($count, $callback, $column = null, $order = 'asc') { $options = $this->getOptions(); - if (isset($options['table'])) { - $table = is_array($options['table']) ? key($options['table']) : $options['table']; - } else { - $table = ''; - } - $column = $column ?: $this->getPk($table); - if (is_array($column)) { - $column = $column[0]; - } + + $column = $column ?: $this->getPk($options); + if (isset($options['order'])) { if (App::$debug) { throw new \LogicException('chunk not support call order'); } unset($options['order']); } - $bind = $this->bind; - $resultSet = $this->options($options)->limit($count)->order($column, $order)->select(); - if (strpos($column, '.')) { - list($alias, $key) = explode('.', $column); + $bind = $this->bind; + if (is_array($column)) { + $times = 1; + $query = $this->options($options)->page($times, $count); } else { - $key = $column; - } - if ($resultSet instanceof Collection) { - $resultSet = $resultSet->all(); + if (strpos($column, '.')) { + list($alias, $key) = explode('.', $column); + } else { + $key = $column; + } + $query = $this->options($options)->limit($count); } + $resultSet = $query->order($column, $order)->select(); while (!empty($resultSet)) { - if (false === call_user_func($callback, $resultSet)) { - return false; - } - $end = end($resultSet); - $lastId = is_array($end) ? $end[$key] : $end->getData($key); - $resultSet = $this->options($options) - ->limit($count) - ->bind($bind) - ->where($column, 'asc' == strtolower($order) ? '>' : '<', $lastId) - ->order($column, $order) - ->select(); if ($resultSet instanceof Collection) { $resultSet = $resultSet->all(); } + + if (false === call_user_func($callback, $resultSet)) { + return false; + } + + if (is_array($column)) { + $times++; + $query = $this->options($options)->page($times, $count); + } else { + $end = end($resultSet); + $lastId = is_array($end) ? $end[$key] : $end->getData($key); + $query = $this->options($options) + ->limit($count) + ->where($column, 'asc' == strtolower($order) ? '>' : '<', $lastId); + } + + $resultSet = $query->bind($bind)->order($column, $order)->select(); + } return true; }