PHP code example of lomocoin / php-mongo-transaction

1. Go to this page and download the library: Download lomocoin/php-mongo-transaction library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lomocoin / php-mongo-transaction example snippets




use Lomocoin\Mongodb\Config\TransactionConfig;
use Lomocoin\Mongodb\Transaction\Transaction;
use MongoDB\Client;

$dbName = 'lomocoin_mongodb_test';
$config = new TransactionConfig(
    new Client(),
    $dbName,
    'lomocoin_mongodb_test_transaction_log',
    'lomocoin_mongodb_test_transaction_state_change_log');

$transaction = Transaction::begin($config);

$collection = $config->getMongoDBClient()->$dbName->testCollection;

$transaction->insertOne($collection, [
    'username' => 'B',
    'email'    => '[email protected]',
    'name'     => 'BB',
]);

$transaction->updateOne($collection, [
    'username' => 'B',
], [
    '$set' => [
        'name' => 'BBB',
    ],
]);

$transaction->deleteOne($collection, ['username' => 'B']);

// if no error happens, you commit
$transaction->commit();

// if any error happens, you rollback
$transaction->rollback();
bash
composer