PHP code example of hema / mongo_transaction

1. Go to this page and download the library: Download hema/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/ */

    

hema / mongo_transaction example snippets


use Hema\MongoTransaction\Transactions\Eloquent\Model

use use Jenssegers\Mongodb\Eloquent\Model 

~$ sudo mongod --replSet "rs0" 

~$ sudo mongo 

use Hema\MongoTransaction\Transactions\Transaction


* Transaction::start();

// ........ A bunch of other db operations

* Transaction::commit();

// ........ commit when success


* Transaction::rollback();

// ........ Or rollback when error occure



* create();
* insert();
* update();
* delete();
* forceDelete(); 
* restore()
* increment();
* decrement();

* trash(); //custome method for soft-delete
* remove(); //custome method for hard-delete




    Transaction::start();

  try {
 	User::insert($some_data);

  	Profile::insert($some_data);

        Transaction::commit();

        return 'done';

   } catch (\Exception $e) {

        Transaction::rollback();

  	return $e->getMessage();
   }