改进Query类的chunk方法支持排序设置

This commit is contained in:
thinkphp
2017-08-19 10:42:05 +08:00
parent 04ee016643
commit 6b98a9974c

View File

@@ -2566,9 +2566,10 @@ class Query
* @param integer $count 每次处理的数据数量 * @param integer $count 每次处理的数据数量
* @param callable $callback 处理回调方法 * @param callable $callback 处理回调方法
* @param string $column 分批处理的字段名 * @param string $column 分批处理的字段名
* @param string $order 排序规则
* @return boolean * @return boolean
*/ */
public function chunk($count, $callback, $column = null) public function chunk($count, $callback, $column = null, $order = 'asc')
{ {
$options = $this->getOptions(); $options = $this->getOptions();
if (isset($options['table'])) { if (isset($options['table'])) {
@@ -2576,9 +2577,12 @@ class Query
} else { } else {
$table = ''; $table = '';
} }
$column = $column ?: $this->getPk($table); $column = $column ?: $this->getPk($table);
if (is_array($column)) {
$column = $column[0];
}
$bind = $this->bind; $bind = $this->bind;
$resultSet = $this->limit($count)->order($column, 'asc')->select(); $resultSet = $this->limit($count)->order($column, $order)->select();
if (strpos($column, '.')) { if (strpos($column, '.')) {
list($alias, $key) = explode('.', $column); list($alias, $key) = explode('.', $column);
} else { } else {
@@ -2597,8 +2601,8 @@ class Query
$resultSet = $this->options($options) $resultSet = $this->options($options)
->limit($count) ->limit($count)
->bind($bind) ->bind($bind)
->where($column, '>', $lastId) ->where($column, 'asc' == $order ? '>' : '<', $lastId)
->order($column, 'asc') ->order($column, $order)
->select(); ->select();
if ($resultSet instanceof Collection) { if ($resultSet instanceof Collection) {
$resultSet = $resultSet->all(); $resultSet = $resultSet->all();