A trigger is a SQL procedure that performs an action when a transaction
(INSERT, UPDATE, DELETE) occurs. You can use triggers to perform validation
of input data, to automatically generate a value for a newly inserted
row, to read from other tables for cross-referencing purposes, or to support
alerts through e-mail messages.
A transaction is a group of SQL statements whose effects are logically
connected. Anything from simple queries to inserting, and deleting operations
can be considered a transaction, as well as more complex groups of several
statements which accomplish a specific task.
A primary key is one or more table columns whose values uniquely identify
each record in a table. In general, a primary key is defined on a single
column, but it is not uncommon to have it defined on two columns.
A session is a way to preserve certain data across subsequent accesses
of the same web application. The session object contains many variables
to store user information on the web server in a location that you choose
in special files. The session support allows you to register arbitrary
numbers of variables to be preserved across requests. Sessions are specific
to individual users. As a result, every user has a separate session and
has access to a separate set of Session variables.
Custom trigger
A custom trigger
is a function whose first parameter is a reference to the executing Transaction.
This allows any trigger to have access to the Transaction context (SQL
fields, primary key
etc.).
If an error is raised inside the trigger it must be returned as an instance
of the tNG_error object.
Here is a sample of a custom trigger that checks if a user is authenticated
or not:
function TriggerBEFORE_checkSession(&$tNG) {
if (!isset($_SESSION['authorised'])) {
$errObj = new tNG_error('User is not authenticated.', array(), array());
return $errObj;
} else {
return null;
}
}
This code uses the following:
a trigger named TriggerBEFORE_checkSession;
as it can be seen from the suggestive naming, it is a BEFORE trigger;
whether or not the user is authenticated is stored
in a session
variable called authorised, and which can be called through the $_SESSION['authorised'] expression (this is PHP syntax);
the error message is returned by using an instance
of the tNG_error object.
A custom trigger can only return NULL, or a transaction
engine error object. If no error is thrown during the trigger execution,
NULL is returned. If an error occurs, the tNG_error object is returned,
which contains the user and developer error (as you can see in the above
code example - the two error messages are the same).