From 7d8ea08194a22aab1ffd28459d30d6dff7cb4470 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Apr 2016 12:46:24 +0800 Subject: [PATCH] =?UTF-8?q?Query=E7=B1=BB=E5=A2=9E=E5=8A=A0chunk=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20=E5=88=86=E6=89=B9=E5=A4=84=E7=90=86=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 116fcbe6..6c40b6ec 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1174,6 +1174,37 @@ class Query return $data; } + /** + * 分批数据返回处理 + * @access public + * @param integer $count 每次处理的数据数量 + * @param callable $callback 处理回调方法 + * @param string $column 分批处理的字段名 + * @param array|Closure $where 查询条件 + * @return array + */ + public function chunk($count, $callback, $column = null, $where = []) + { + $column = $column ?: $this->connection->getTableInfo('', 'pk'); + $model = isset($this->options['model']) ? $this->options['model'] : ''; + $resultSet = $this->limit($count)->where($where)->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->model($model) + ->limit($count) + ->where($where) + ->where($column, '>', $lastId) + ->order($column, 'asc') + ->select(); + } + return true; + } + /** * 获取绑定的参数 并清空 * @access public