Remove associated contacts: Delete Detail Records

When you've created the company delete page in a previous section of this tutorial, the only thing implemented was a simple delete from the company table. Indeed, this removes the selected company from the database , but what happens to the contacts associated to the company? The answer is absolutely nothing. This means you will be stuck with some contact data you actually can't access from the site, and you don't need. These are called orphan records, as their parent company has been deleted.

One way to solve this problem is to fire up your database server management utility (PHPMyAdmin, Microsoft Access etc) and manually delete the remaining records. But this means an extra operation, one which is unnecessary.

The other (simpler) way is to use ImpAKT3's Delete Detail Records trigger . This trigger can only be used on pages that contain a delete operation, and allows implementing a cascade delete: all records in a detail table, associated with the current record that is being deleted, will be erased too.

To add this functionality to your site, follow the next steps:

  1. Open the delete page from the company folder. Remember that you can only use this server behavior on a page containing a delete transaction for the master table.
  2. With the delete page opened in Dreamweaver's editable area, apply the Delete Detail Records server behavior. To access it, click on the Plus (+) button of the Server Behaviors tab, then open MX Kollection -> Form Validation -> Delete Detail Records. If the Server Behaviors tab is not visible, open it from Window -> Server Behaviors.
  3. Once the server behavior's dialog box opens, configure it to work on the desired detail table, in the way you want it to. Setting up the options is quite easy, and you can follow the instructions below:
    · Notice first that the interface is divided into two tabs: Basic, which allows you to set the tables, and field keys, and Advanced, where more in depth options regarding the trigger's properties can be set. Of interest right now is the Basic tab, as all trigger properties are automatically configured.
    · The first section of the Basic tab contains fields regarding the master (transaction) table. These fields are automatically retrieved from the delete transaction that already exists on page: the company_com table, with the id_com primary key field.
    · The second section is where you define the detail table.
    · In the Detail table drop-down menu select the table to act as detail; in this case, it's the contacts table: contact_con.
    · In the Detail foreign key drop-down menu, select the field that acts as a link between the master and detail table: idcom_con.
    · If you've used the same names, the dialog box looks like this:



    · Now you can click on the OK button to add the server behavior to the page.
  4. This is all it takes to implement the removal of associated contacts,along with the company they belong to. The final step is to edit the page header, so that it will suggest that contacts will be deleted too. Change the header text to "Are you sure you want to delete the following company and its associated contacts?" (without the quotes).
  5. Save your page and upload it to the server. When you will attempt to delete a company in the future, all its contacts will be removed too. You can check this by opening your database server management utility, and browsing through the contacts table, before and after the delete operation.

Now you can move on to the next topic, where you will learn how to make your contact insertion page bullet proof, by adding a mechanism to prevent adding a contact for a company that doesn't exist.