imactool / hyperf-batch
insert and update batch(bulk) in Hyperf
Requires
- php: ^8.4
- hyperf/db-connection: ~3.1.0
- hyperf/support: ~3.1.0
- nesbot/carbon: ^2.73
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.84
- hyperf/config: ~3.1.0
- hyperf/database-sqlite: ~3.1.0
- mockery/mockery: ^1.6
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5.40
- swoole/ide-helper: ~6.1.0
Suggests
- swow/swow: Required to create swow components.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-02 10:20:20 UTC
README
Insert and update batch (bulk) in hyperf
From
modify from mavinoo/laravel-batch,a coroutine-based
Install
composer require imactool/hyperf-batch
Requirements and upgrading
- PHP 8.4 or later within PHP 8.x.
- Hyperf 3.1 components (
~3.1.0); Composer installs the required database and support components. - A compatible coroutine runtime: Swoole, or Swow with
hyperf/engine-swow. - A PDO driver for your database. The integration suite covers MySQL 8.0 and SQLite.
This release raises the PHP minimum from 7.3 to 8.4 and targets Hyperf 3.1.
Update the application runtime before upgrading the package. Existing Batch::update(),
Batch::updateWithTwoIndex(), Batch::insert() and batch() calls remain available.
The helper uses Hyperf\Support\make, so hyperf/helper is not required.
Operations use the model's actual connection for driver selection, table prefixes,
SQL execution and insert transactions, including models using a non-default connection.
Empty or malformed insert rows return false; every row must match the column count.
Two-index updates require a non-empty second index and exclude both index columns from
the update. Missing second indexes raise InvalidArgumentException for non-empty input.
PHP 8.4's array_all() validates insert rows. Source syntax stays compatible with
Hyperf 3.1's PHP-Parser 4.x dependency; property hooks and asymmetric visibility are
not used. PHPUnit 10.5 is retained because newer PHPUnit lines require PHP-Parser 5.x.
Example Update 1
use App\Models\User; use Ylnwqm\HyperfBatch\Batch; $userInstance = new User; $value = [ [ 'id' => 1, 'status' => 'active', 'nickname' => 'Mohammad' ] , [ 'id' => 5, 'status' => 'deactive', 'nickname' => 'Ghanbari' ] , ]; $index = 'id'; Batch::update($userInstance, $value, $index);
Example Update 2
use App\Models\User; $userInstance = new User; $value = [ [ 'id' => 1, 'status' => 'active' ], [ 'id' => 5, 'status' => 'deactive', 'nickname' => 'Ghanbari' ], [ 'id' => 10, 'status' => 'active', 'date' => Carbon::now() ], [ 'id' => 11, 'username' => 'mavinoo' ] ]; $index = 'id'; Batch::update($userInstance, $value, $index);
Example Increment / Decrement
use App\Models\User; $userInstance = new User; $value = [ [ 'id' => 1, 'balance' => ['+', 500] // Add ] , [ 'id' => 2, 'balance' => ['-', 200] // Subtract ] , [ 'id' => 3, 'balance' => ['*', 5] // Multiply ] , [ 'id' => 4, 'balance' => ['/', 2] // Divide ] , [ 'id' => 5, 'balance' => ['%', 2] // Modulo ] , ]; $index = 'id'; Batch::update($userInstance, $value, $index);
Example Insert
use App\Models\User; $userInstance = new User; $columns = [ 'firstName', 'lastName', 'email', 'isActive', 'status', ]; $values = [ [ 'Mohammad', 'Ghanbari', 'emailSample_1@gmail.com', '1', '0', ] , [ 'Saeed', 'Mohammadi', 'emailSample_2@gmail.com', '1', '0', ] , [ 'Avin', 'Ghanbari', 'emailSample_3@gmail.com', '1', '0', ] , ]; $batchSize = 500; // insert 500 (default), 100 minimum rows in one query $result = Batch::insert($userInstance, $columns, $values, $batchSize);
// result : false or array sample array result: Array ( [totalRows] => 384 [totalBatch] => 500 [totalQuery] => 1 )
Helper batch()
// ex: update $result = batch()->update($userInstance, $value, $index); // ex: insert $result = batch()->insert($userInstance, $columns, $values, $batchSize);
Tests
From this repository, install development dependencies and run:
composer install
composer test
composer analyse
The default suite uses real Hyperf 3.1 components, its DI container and in-memory SQLite databases. It checks batch results, timestamps, arithmetic updates, two-index updates, facade/helper calls, input validation and rollback on a non-default connection. It also boots Hyperf's database providers and exercises the real connection pool in a coroutine, checking rollback and connection release. PHP warnings, notices and deprecations fail the suite.
To run the same tests against a disposable MySQL database:
BATCH_TEST_DB_DRIVER=mysql \
BATCH_TEST_DB_HOST=127.0.0.1 \
BATCH_TEST_DB_PORT=3306 \
BATCH_TEST_DB_DATABASE=hyperf_batch_test \
BATCH_TEST_DB_USERNAME=batch \
BATCH_TEST_DB_PASSWORD=batch \
composer test
The MySQL test account needs permission to create and drop test tables. CI runs PHP
8.4 with Swoole 6.1.11 and Swow 1.6.2. The Swow job installs
hyperf/engine-swow:2.14.0 to select the matching coroutine engine.