Create custom transactions

A Custom Transaction can have as main operation any SQL statement or no SQL statement at all, just a set of instructions. It is generally used to execute more complex INSERT/UPDATE/DELETE statements.
Let's build a custom transaction that executes an insert statement and then redirects to another page using some of the submitted values.

We'll start the page by including the Transaction Engine classes:

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

 

And then create the unique page dispatcher object:

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("");

 

The standard connection is transformed into a Transaction Engine connection and made available through a variable:

// Make unified connection variable
$conn_localhost = new tNG_connection($localhost, $database_localhost);

 

Next comes the code for defining the custom triggers, but they will be displayed in detail later.
After this, the actual Custom Transaction is instantiated and the custom triggers are added to the new Transaction:

// make an instance of the transaction object
$customTransaction1 = new tNG_custom($conn_test);
$tNGs->addTransaction($customTransaction1);

// register triggers
$customTransaction1->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "MM_custom");
$customTransaction1->registerTrigger("BEFORE", "TriggerBEFORE_AddField1", 1);
$customTransaction1->registerTrigger("BEFORE", "Trigger_Default_Validate", 1, $Validate1);
$customTransaction1->registerConditionalTrigger(@$_POST['cb'] == 1, "END", "TriggerEND_Redirect1", 98);
$customTransaction1->registerTrigger("END", "TriggerEND_Redirect1", 99);

 

The above code is similar to that of a classic insert. What comes next is particular for the custom transaction: You can set the actual SQL string to be executed:

// set transaction SQL
$customTransaction1->setSQL("INSERT INTO users (UserID, Forename,
Surname, Email, Level) VALUES ({UserID}, {Forename}, {Surname},{Email}, {Level})");

 

The above SQL statement is different from a classic SQL insert statement because it doesn't contain values to be inserted, but placeholders, or mark-up language, such as {FieldVariable}. They are replaced with their corresponding values at runtime because the Transaction has columns registered to it and knows how to get the values:

$customTransaction1->addColumn("Surname", "STRING_TYPE", "POST", "Surname", "");
$customTransaction1->addColumn("Email", "STRING_TYPE", "POST", "Email", "default@default.com");
$customTransaction1->addColumn("Level", "NUMERIC_TYPE", "POST", "Level", 2);
$customTransaction1->addColumn("UserID", "STRING_TYPE", "POST", "UserID", "");

 

Finally, execute the transaction and get the result Recordset:

$tNGs->executeTransactions();
$rsCustom = $tNGs->getRecordset("custom");
$row_rsCustom = mysql_fetch_assoc($rsCustom);
$totalRows_rsCustom = mysql_num_rows($rsCustom);

 

As you can see in the above code, the getRecordset() method of the dispatcher doesn't receive a table name as parameter (like in the insert/update transactions), but a default string (“custom”).