TriggersTriggers are methods that can be defined to carry out custom behaviors when certain events occur in the application (e.g., when records are saved, inserted, or deleted).
Triggers are generally regarded as one of the more advanced features
of database applications. Despite the "advanced" status of
triggers, however, they are very simple to use and can be leveraged by
Dataface developers to add powerful functionality to their applications. So what can you do with a trigger?
You can really do just about anything you want with a trigger. A trigger is essentially just a custom PHP method that is called by Dataface when certain events occur. What triggers are available?As of version 0.5.3 Dataface supports the following triggers:
How do you define a trigger?Triggers are always defined as methods of the delegate class for a
table. As an example, suppose we wanted to add a trigger to send a the
administrator a notification email every time a new record is inserted
into the 'Course' table. The steps would be as follows:
Sending feedback to the userThe above example sends the email without any feedback to the application's user of whether the email succeeded or not. When the user inserts a new Course he will only see that the record was succesfully saved, but no mention is made of the email. The following example does the same thing as the previous one, except that it sends a confirmation to the user when the email has been successfully sent: function afterInsert(&$record){ Now, when the user inserts a new record he will see a confirmation as follows: Notice that we used the Dataface_Application::getResponse() function
to obtain a reference to the application's response array. The
response array is just a global array that is shared by the entire
application that allows you to pass information easily from one part of
the application to another. The '--msg' index of array is where
you can place text that you wish to have displayed to the user as a
confirmation. Note: It is important to use '=&' to assign the result of Dataface::getResponse() and not just '='. e.g.: $response =& Dataface_Application::getResponse(); // correct! This is because we need a reference to the response array,
not a copy. That way when we assign a value to the '--msg' index
it is applied to the actual response array and not a copy of it.
Handling ErrorsThe above method of sending messages to the user is useful for
notifications and confirmations. However, in some cases you want
to inform the user that an error occurred and cancel further
operations. For example you may want to disallow a record to be
inserted if a confirmation email cannot be sent. In this case we
define the beforeInsert() trigger and return a PEAR_Error object if the email fails as follows: <? This trigger is called just before a course record is inserted into
the database. If the mail succeeds, then the user sees a success
message. If it fails, on the other hand, the insert is cancelled,
and the user will see a message as follows:
Note: Notice the use of the DATAFACE_E_NOTICE constant as a second parameter for PEAR::raiseError(). This is important as without it Dataface will display a less user-friendly error message complete with stack trace. If the error is such that you want a complete stack trace, you can use the DATAFACE_E_ERROR constant instead.
|