Planning a method for editing records in linked tables
There's one key issue that needs to be planned very carefully before moving forward with the actual code. Right now, we have good functional editing for the records in a single db table, and we can also trawl out and display linked records from another table; but there's no strategy yet for editing the records from a linked table. The fileLink table provides a good simple use-case for developing this code, because it's fairly straightforward, but we also need to be able to extend any objects and routines we write for this case to the more complicated looming issue of the user-defined fields. So here's an outline of the problem, to help me concentrate:
- In normal record-editing, you harvest the fields of a record from a db table, and present them to the user for editing in a form; then you submit the form (or submit a structure built from the form) to the db, and amend or append the record.
- In the case of this particular db, we present multiple records in the form of an HTML table, and if you have the right to edit, then the table has an Add button to add new records, and a set of View, Edit and Delete buttons for each row in the table. When you click on the Edit button or the Add button, you get a form to modify or complete.
- When there is another db table which has records linked to this particular record -- in the concrete example here, when one or more files is linked to a document record -- then those records are managed through another record list object which is owned by the record object for the document. They too can be displayed in a table, embedded inside the view or edit displays of a regular record.
- When you're EDITING a document record, therefore, you'll see a table of file links inside it, and that table should have an Add button of its own, along with View, Edit and Delete buttons for each of the existing file links.
- The big questions are these:
- If you Add, Edit or Delete items in this subtable, should those operations be done "live", or should they be somehow stored in the client GUI, and submitted along with the main record that they're "part of"? Most systems seem to do the former, but that means that if you abandon or cancel changes to the main document record, your changes to the file link table will have to be undone; which means they'll have to be carefully tracked. On the other hand, if it's possible to mimick or store up those changes to the file links table, and do them at the same time as the changes to the main document record, you'd be able to back out of them easily. Also, some of these changes might be impossible outside the context of a single, unified submission; if, for instance, you're adding a new document record, then you have no document id for the moment, and you can't create a record in the file link table which references a non-existent document id. So in order to be able to process both New and Edit operations in the same way, and to allow for backing out elegantly, it appears we should process the entire set of changes at the same time, when submitting the document record item.
- What, then, should the underlying XHTML form structure be? These are the options:
- All the form elements for the parent record and the related-table records form part of a single XHTML form. This requires careful organization to avoid duplicate ids, and to make the submission data easily parsable on the back end.
- Each related-record item is contained in its own separate form. This actually gains us nothing, because we still have the issue of duplicate ids to handle, and we now have the additional problem of multiple forms to manage.
- We also have to decide at what level in the object hierarchy the information about whether a record is embedded or not is stored, and how it is set (or how the condition is detected).
- I have an existing concern that the ability to edit any, and more than one, record in a display table at the same time might result in forms or fields being AJAXed into the page which have duplicate ids. So first we need to check whether that's the case or not, with regard to simple tables (with no linked records from another table).
- It might make sense to start constructing all form element ids in such a way that they are guaranteed to be distinct, and easily parsable. That could be managed with a dot-separated pattern like this:
tableName.primaryKey.fieldName
- The wrinkle here is that for "new" items, the primary key is currently defined as -1, meaning that they don't have one; in the case of embedded records from related tables, we might have multiple New items during a single editing operation of the parent record, so we'd have multiple items with -1 as their id. We could, instead, go for a pattern which uses 0 for a new record instead of -1, and looks like this:
tableName.primaryKey.fieldname[.autoUnique]
where we generate autoUnique on the fly in the client when a proposed new item is created. Thus we can easily see when an item is new (the primary key bit of the id is 0, and we avoid having a minus/hyphen in the middle of an id), and at the same time, the discardable autoUnique component guarantees that ids aren't duplicated. - The back-end code would then have to sort the submission, which could be of significant size, into groups based on the table name, and then into groups based on the primaryKey. It would then:
- Submit changes to the main, parent record (in our current case, the document record) first. This enables it to get an id in the case of a new record.
- Trawl through all its related tables removing any existing records which reference this item (necessary, because related table records may have been "deleted" in the browser GUI, and will then have no related submitted content. This raises another issue: it's clear that the user may choose to delete all the related items from a related table, leaving nothing to be submitted; so the back-end object which handles the submission must know, independently of the submission itself, about all tables which contain related items. This should probably be the case anyway. It suggests that the routine that cleans up all related table items could be a generic one that is coded right into the MdhRecord object.
- Go through each of the related table items in the submission, and create new records for them, (using in the case of New the parent (document) id resulting from the first operation, or the existing one in the case of an Edit).
Phew. I think that's it, for the basics. If I can get this working as planned, it'll make coding the user fields a bit easier.