A typical insert transaction uses code like the snippet below, to achieve its purpose: inserting a set of values into a database table.
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. In order to create
a new dispatcher,
the generated code creates a new instance of the dispatcher object, passing
it as parameter the file's path relative to the site root (e.g. for a
file in the /files folder, the path is ../):
// 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 actual transaction type: an insert one.
As you can see, all is done OOP;
creating a new insert transaction resumes to instantiating
a class. 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 insert transaction instance
$ins_company_com = new tNG_insert($conn_localhost);
$tNGs->addTransaction($ins_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
$ins_company_com->registerTrigger("STARTER", "Trigger_Default_Starter",
1, "POST", "MM_Insert1");
$ins_company_com->registerTrigger("BEFORE", "Trigger_Default_FormValidation",
10, $formValidation);
$ins_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_Redirect($this,"index.php");
The next section of code assigns the table into which the insert 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, reference and default value:
// Add columns
$ins_company_com->setTable("company_com");
$ins_company_com->addColumn("name_com", "STRING_TYPE",
"POST", "name_com");
$ins_company_com->addColumn("address_com", "STRING_TYPE",
"POST", "address_com");
$ins_company_com->addColumn("active_com", "NUMERIC_TYPE",
"POST", "active_com");
$ins_company_com->setPrimaryKey("id_com", "NUMERIC_TYPE");
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);