Update transaction

The Transaction Engine 3 update transaction is similar in terms of code to the insert transaction, the main difference is the class that's being instanced. The code looks like illustrated below, with the associated explanations.

The first thing that occurs when adding a new transaction into a page is the inclusion of the Transaction Engine 3 classes. They are included in a file called tNG.inc.php, in the includes/tng folder on the server. This folder is created automatically when using a Transaction Engine 3 feature in one of the site's pages:

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

 

As you can have multiple transactions on page, each one must register with the page's unique dispatcher, so that it can establish a coherent order of execution:

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

 

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

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

 

The triggers for this page are now created. For form validations, the tNG_FormValidation trigger is used.

The fields that are required for validation will be added using the addField method. If the field does not require any validation, it will not be added to the tNG_FormValidation object. After the fields are added, the Dispatcher is instructed to prepare the validation:

// Start trigger
$formValidation = new tNG_FormValidation($tNGs);
$tNGs->prepareValidation($formValidation);
// End trigger

 

This code section creates the update transaction by creating an instance of the update record object. Aside creating the transaction, it is also registered to the dispatcher, by the addTransaction method, that takes the name of the transaction as a parameter:

// Make an update transaction instance
$upd_company_com = new tNG_update($conn_localhost);
$tNGs->addTransaction($upd_company_com);

 

To add Triggers to the Transaction, the tNG->registerTrigger() method is used. The first three parameters are required, (Trigger type, Trigger name, Trigger priority), and the last are optional and represent the parameters used by the Transaction to call the Trigger. Besides these extra parameters, the first parameter always passed when calling a Trigger is the reference to the instance of the current executing Transaction. In this way, the trigger has access to the Transaction context:

// Register triggers
$upd_company_com->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "MM_Update1");
$upd_company_com->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_company_com->registerTrigger("END", "Trigger_Default_Redirect", 99, index.php);

 

For example, in this case, the transaction will execute the trigger Trigger_Default_Redirect like this:

Trigger_Default_Rediret($this,"index.php");

 

The next section of code assigns the table into which the update will occur (in the first line) and then adds the actual columns to the table. The last line defines the primary key of the table: the field name, and its type. When adding a column, the arguments passed to the addColumn method are: the column name, column type, method of passing the value and the reference:

// Add columns
$upd_company_com->setTable("company_com");
$upd_company_com->addColumn("name_com", "STRING_TYPE", "POST", "name_com");
$upd_company_com->addColumn("address_com", "STRING_TYPE", "POST", "address_com");
$upd_company_com->addColumn("active_com", "NUMERIC_TYPE", "POST", "active_com");
$upd_company_com->setPrimaryKey("id_com", "NUMERIC_TYPE", "GET", "id_com");

 

When using the addColumn method, there are several column types that can be used:

 

There are also several reference methods to use with the addColumn() call:

 

After all Transactions with their corresponding triggers were set and added to the Dispatcher, it can be executed:

// Execute all the registered transactions
$tNGs->executeTransactions();

 

The Dispatcher will execute each Transaction by calling the executeTransaction() method for each of them.

If any of the Transactions will export a recordset , we can ask the Dispatcher to search and retrieve it from its Transactions:

// Get the transaction recordset
$rscompany_com = $tNGs->getRecordset("company_com");
$row_rscompany_com = mysql_fetch_assoc($rscompany_com);
$totalRows_rscompany_com = mysql_num_rows($rscompany_com);