src/Database/DatabaseUtil.php
public function processInPieces(string $countQuery, string $query, $callback = null, string $key = null, int $bulkSize = 5000)
    {
        /** @var Database $database */
        $database = $this->framework->createInstance(Database::class);
        $total = $database->execute($countQuery);

        if ($total->total < 1) {
            return false;
        }

        $bulkSize = (int) $bulkSize;
        $totalCount = $total->total;
        $cycles = $totalCount / $bulkSize;

        for ($i = 0; $i <= $cycles; ++$i) {
            $result = $database->prepare($query)->limit($bulkSize, $i * $bulkSize)->execute();

            if ($result->numRows < 1) {
                return false;
            }

            if (\is_callable($callback)) {
                $return = [];

                while (false !== ($row = $result->fetchAssoc())) {
                    if ($key) {
                        if (isset($row[$key])) {
                            $return[$row[$key]] = $row;
                        }

                        continue;
                    }

                    $return[] = $row;
                }

                \call_user_func_array($callback, [$return]);
            }
        }

        return $totalCount;
    }
public function processInPieces(string $countQuery, string $query, $callback = null, string $key = null, int $bulkSize = 5000)
    {
        /** @var Database $database */
        $database = $this->framework->createInstance(Database::class);
        $total = $database->execute($countQuery);

        if ($total->total < 1) {
            return false;
        }

        $bulkSize = (int) $bulkSize;
        $totalCount = $total->total;
        $cycles = $totalCount / $bulkSize;

        for ($i = 0; $i <= $cycles; ++$i) {
            $result = $database->prepare($query)->limit($bulkSize, $i * $bulkSize)->execute();

            if ($result->numRows < 1) {
                return false;
            }

            if (\is_callable($callback)) {
                $return = [];

                while (false !== ($row = $result->fetchAssoc())) {
                    if ($key) {
                        if (isset($row[$key])) {
                            $return[$row[$key]] = $row;
                        }

                        continue;
                    }

                    $return[] = $row;
                }

                \call_user_func_array($callback, [$return]);
            }
        }

        return $totalCount;
    }
public function processInPieces ( string $ countQuery , string $ query , $ callback = null , string $ key = null , int $ bulkSize = 5000 ) { /** @var Database $database */ $ database = $ this -> framework -> createInstance ( Database :: class ) ; $ total = $ database -> execute ( $ countQuery ) ; if ( $ total -> total < 1 ) { return false ; } $ bulkSize = ( int ) $ bulkSize ; $ totalCount = $ total -> total ; $ cycles = $ totalCount / $ bulkSize ; for ( $ i = 0 ; $ i <= $ cycles ; ++ $ i ) { $ result = $ database -> prepare ( $ query ) -> limit ( $ bulkSize , $ i * $ bulkSize ) -> execute ( ) ; if ( $ result -> numRows < 1 ) { return false ; } if ( \ is_callable ( $ callback ) ) { $ return = [ ] ; while ( false !== ( $ row = $ result -> fetchAssoc ( ) ) ) { if ( $ key ) { if ( isset ( $ row [ $ key ] ) ) { $ return [ $ row [ $ key ] ] = $ row ; } continue ; } $ return [ ] = $ row ; } \ call_user_func_array ( $ callback , [ $ return ] ) ; } } return $ totalCount ; }
Process a query in pieces, run callback within each cycle.

@param string   $countQuery The query that count the total rows, must contain "Select COUNT(*) as total"
@param string   $query      The query, with the rows that should be iterated over
@param callable $callback   A callback that should be triggered after each cycle, contains $arrRows of current cycle
@param string   $key        The key of the value that should be set as key identifier for the returned result array entries
@param int      $bulkSize   The bulk size

@return bool|int False if nothing to do, otherwise return the total number of processes entities
Process a query in pieces run callback within each cycle .