When adding entries in a detail table where the user must manually enter the foreign key value, accidents are prone to happen. Also, when you use a drop-down menu, or any other way of deciding the foreign key value, there is still the risk that between the page load, with the master record options, and the actual insert, another user will delete the master record. Since the retrieval of the options and the insert operation are two different SQL transactions, and there will always be some time interval between them, something might go wrong.
This is why you should check for the existence of the master recordA database row from the master table. The master table is defined by a one-to-many relation to another table (the detail table) before the actual insert. MX Kollection 3 offers yet another tool to perform this task: the Check Master Record server behavior .
To add this functionality to your site, and thus make the insert contact page bullet proof, follow the next steps:
First off, open the insert page from the contact folder in Dreamweaver's editable area.
You should already have the Insert transaction on page, from the previous section of this tutorial. This is needed, as the Check Master Record uses transaction data for its configuration.
Apply the Check Master Record server behavior. Access it by clicking on the Plus (+) button on the server behaviors tab, and then going to MX Kollection -> Form Validation -> Check Master Record.
Configure the server behavior, by setting the options in the dialog box that opens. The user interface is divided into two tabs: Basic, containing options about the tables and fields to use, and Advanced, with options regarding the trigger properties. As the latter is automatically set up, you will only have to deal with the Basic options. The instructions below will guide you to quickly configure the trigger:
· The Transaction table drop-down is automatically completed with the table used in the insert transaction (contact_con for this case), and disabled.
· In the Foreign keyA foreign key is a field from a database table that refers to (or targets) a specific key, usually the primary key, in another table. This is a convenient way of logically linking information from related tables in the same database. For instance, a table that stores information about products can contain a foreign key that references the primary key field in a table that stores manufacturers. This way, each product has an associated manufacturer – its associated foreign key points to the unique identifier of the manufacturer. Please note that the foreign key is not unique, but the referenced field (the primary key in the referenced table) usually is. drop-down menu you must select the transaction table's field that ensures the link to the master table. For this particular case, the field is idcom_con.
· In the Master table drop-down menu select the database table that acts as master for the transaction table. Select company_com.
· In the Master primary key drop-down menu select the master table's field containing the primary key: id_com.
· You will also have to enter an Error message, that will be displayed when the master record check fails. Enter a suggestive text (e.g. "The selected company does not exist!").
· If you've followed the instructions above, the dialog box should look like this:
Click on the OK button to add the server behavior to the page.
You can now save your page and upload it to the server. If you wish to test the newly added server behavior, open the insert page in a browser window, and in another window delete the company. When submitting a new contact for that company, you will receive an error message.