mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 15:42:48 +08:00
改进chunk方法支持复合主键
This commit is contained in:
@@ -2605,47 +2605,51 @@ 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');
|
||||||
}
|
}
|
||||||
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)) {
|
||||||
if (strpos($column, '.')) {
|
$times = 1;
|
||||||
list($alias, $key) = explode('.', $column);
|
$query = $this->options($options)->page($times, $count);
|
||||||
} else {
|
} else {
|
||||||
$key = $column;
|
if (strpos($column, '.')) {
|
||||||
}
|
list($alias, $key) = explode('.', $column);
|
||||||
if ($resultSet instanceof Collection) {
|
} else {
|
||||||
$resultSet = $resultSet->all();
|
$key = $column;
|
||||||
|
}
|
||||||
|
$query = $this->options($options)->limit($count);
|
||||||
}
|
}
|
||||||
|
$resultSet = $query->order($column, $order)->select();
|
||||||
|
|
||||||
while (!empty($resultSet)) {
|
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) {
|
if ($resultSet instanceof Collection) {
|
||||||
$resultSet = $resultSet->all();
|
$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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user