Archive for November 14th, 2005

DB_DataObject_FormBuilder reverseLink SubForms

November 14th, 2005

A while ago I needed the ability to create sub-forms for a html form I was building.

Justin Patrin had built a PEAR HTML_QuickForm element for handling SubForms so this was a simple task, but when I started coupling forms to my database objects there was something missing.

My data was modeled in the database with one main record, and many small records in other tables that linked back to the main record (foreign keys on a one to many model). Nothing complex, but I began to realize that while DB_DataObject_FormBuilder was a great tool – it couldn’t model my data entirely within one form . To create those reverseLink records I would have to insert the main record, and then render a separate form for the sub records. An alternative would be to manually add in an extra subform element on a per DataObject class basis.. but DB_DataObject knows the structure of my database, why can’t FormBuilder generate a form that models it?

Well, I wrote up some patches for FormBuilder.php to enable SubForms on reverseLinks… and it’s in cvs as of yesterday.

Here’s an example of how to turn it on with data structured as follows: Two tables, reports and vehicles. 1 to many on reports->vehicles.

(links.ini)
[vehicles]
reportid = reports:reportid

To turn ON reverseLink SubForms, add this to the DataObject class for your table:

class DataObjects_Reports extends DB_DataObject
{

// ... All the normal autogenerated stuff here.

var $fb_reverseLinks = array(array('table'=>'vehicles'));
var $fb_reverseLinkNewValue = true; // To indicate a blank subform should be added for a new reverseLink record
var $fb_linkElementTypes = array('__reverseLink_vehicles_reportid'=>'subForm'); // Tell FB to make this link a subForm instead of the default checkbox.

}

And voila, one form with all the reverseLink records editable including the ability to make a new linked record.

Big thanks to Justin Patrin for supporting this idea of mine, and touching up my patch to get it into FormBuilder.