This repository was archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Batch (Transaction)
Kerem Güneş edited this page Jan 5, 2017
·
7 revisions
Batch object designed like other general transaction stuff, but also has a method named as queue to queue queries before do call. "Do" will handle each query separately (e.g: setting last id or affected rows).
Single Transaction
// get batch object
$batch = $agent->getBatch();
// set autocommit=0 (not needed for pgsql)
$batch->lock();
try {
// commit
$batch->doQuery('insert into `users` values(null,?,?)', ['John', 25]);
} catch (\Throwable $e) {
// rollback
$batch->cancel();
}
// set autocommit=1 (not needed for pgsql)
$batch->unlock();
// get insert id if success
$result = $batch->getResult();
if ($result) {
dump $result->getId();
}
// remove query queue and empty result array
$batch->reset();Bulk Transaction
// get batch object
$batch = $agent->getBatch();
// set autocommit=0 (not needed for pgsql)
$batch->lock();
try {
$batch->queue('insert into `users` values(null,?,?)', ['John', 25]);
$batch->queue('insert into `users` values(null,?,?)', ['Boby', 35]);
$batch->queue('insert into `uzerz` values(null,?,?)', ['Eric', 15]); // boom!
// commit
$batch->do();
} catch (\Throwable $e) {
dump $e->getMessage();
// rollback
$batch->cancel();
}
// set autocommit=1 (not needed for pgsql)
$batch->unlock();
// get insert ids if success
foreach ($batch->getResults() as $result) {
dump $result->getId();
}
// or
dump $batch->getResultsIds(/* bool $merge=true */);
// remove query queue and empty result array
$batch->reset();Process Time
dump $batch->getTotalTime();