Triggers are files that are included via include() statement. SQL triggers are included before or after insert, update, or delete of record. FORM triggers are included before displaying the form that will allow the corresponding operation, or after the user canceled this form.
There are following types of phpMyEdit triggers:
'pre' triggers are usually used to check conditions before displaying the operation's page. For example, users may be allowed to View all records but can only Edit a subset of them. Another usage is to lock the record in order to avoid other users to start to change it at the same time.
'before' triggers are usually used to verify conditions prior to executing the main operation. For example, they can be of some use for input validation.
'after' triggers are usually used to perform follow-up operations after the main operation. For example, to update secondary tables to enforce referential integrity or to update aggregate tables.
'cancel' triggers are usually used to perform follow-up operations after users cancel the form. For example, if a record is locked using a 'pre' triggers, then a 'cancel' trigger can unlock it.
If 'pre' triggers fail, users are sent back to the list, except for the 'update' case, where users are sent back to view page if the pre-update trigger fails.
For SQL triggers, the operation sequence is this: before, main, after. If any operation fails, not only should the next operation(s) not be executed, but the previous ones are 'rolled back' as if they never happened. If a database is not able to do this, it is not 'transaction-safe'.
Triggers are risky in basic MySQL as there is no native transaction support. It is not transaction-safe by default. There are transaction-safe table types in MySQL that can be conditionally built (see MySQL-Max), but phpMyEdit is currently not set up to support real transactions. What that means is that if an operation fails, the database may be left in an intermediate and invalid state.
The programmer must understand and accept these risks prior to using the phpMyEdit triggers mechanism. If the triggers are used, they execute within the namespace or scope of the phpMyEdit class.
Triggers must return true or false to indicate success or failure.
Example 3-17. Select triggers
// Before displaying the view page $opts['triggers']['select']['pre'] = 'categories.TSP.inc'; // After canceling the view page $opts['triggers']['select']['cancel'] = 'categories.TSC.inc';
Example 3-18. Insert triggers
// Before displaying the add/copy page $opts['triggers']['insert']['pre'] = 'categories.TIP.inc'; // After requesting save or more in the add/copy page $opts['triggers']['insert']['before'] = 'categories.TIB.inc'; $opts['triggers']['insert']['after'] = 'categories.TIA.inc'; // After canceling the add/copy page $opts['triggers']['insert']['cancel'] = 'categories.TIC.inc';
Example 3-19. Update triggers
// Before displaying the edit page $opts['triggers']['update']['pre'] = 'categories.TUP.inc'; // After requesting save or apply in the edit page $opts['triggers']['update']['before'] = 'categories.TUB.inc'; $opts['triggers']['update']['after'] = 'categories.TUA.inc'; // After canceling the edit page $opts['triggers']['update']['cancel'] = 'categories.TUC.inc';
Example 3-20. Delete triggers
// Before displaying the delete page $opts['triggers']['delete']['pre'] = 'categories.TDP.inc'; // After requesting delete in the delete page $opts['triggers']['delete']['before'] = 'categories.TDB.inc'; $opts['triggers']['delete']['after'] = 'categories.TDA.inc'; // After canceling the delete page $opts['triggers']['delete']['cancel'] = 'categories.TDC.inc';
Please note that ['select']['after'] and ['select']['before'] triggers currently do not exist.
In the following sample are steps during a View, Edit, Apply and Cancel operation described. All involved triggers return true.
In every trigger file you have available following usable variables. Some of them affect only a particular action.
$this | object reference |
$this->dbh | initialized MySQL database handle |
$this->key | primary key name |
$this->key_type | primary key type |
$this->key_delim | primary key deliminator |
$this->rec | primary key value (update and delete only) |
$newvals | associative array of new values (update and insert only) |
$oldvals | associative array of old values (update and delete only) |
$changed | array of keys with changed values |
It is recommended to use the $this->myQuery() method in order to perform database queries for fetching additional data or doing inserts or updates to other database tables.