改进chunk方法支持复合主键

This commit is contained in:
thinkphp
2017-12-27 16:01:15 +08:00
parent b698ab8809
commit 0b028c645c

View File

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