Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to structure my objects to clean up my controller code
    text
    copied!<p>I am trying to build an object to represent a ridiculously large form for a project I am working on. <a href="http://www.flickr.com/photos/namtax/5351091239/" rel="nofollow">http://www.flickr.com/photos/namtax/5351091239/</a></p> <p>I didnt want to build all the fields into one large object as I didnt think this was a good way to do things, so I have structured the model as so - </p> <p>One contact object. </p> <ul> <li><p>One work object - linked to the contact one to one. </p></li> <li><p>One travel object - linked to the contact one to one.</p></li> <li><p>One address object - linked to the contact one to many.</p></li> <li><p>One emergency contact object - linked to the contact one to many.</p></li> </ul> <p>Here is the sample code for one of these relationships - written in CF9 ORM</p> <pre><code> property name="work" fieldtype="one-to-one" cfc="work" mappedby="contact"; </code></pre> <p>The issue here is that it causes the saveContact function in my controller to be large and code smelly due to all the possible options you can choose in the form. Here is a rough translation of the code</p> <pre><code>VARIABLES.contact = contactService.getContact(form.iContactId); contactService.set(contact:contact,argumentCollection:form); contact = contactService.save(contact); if(_emergencyContactService.userIsSubmittingAnEmergencyContact(form)){ VARIABLES.emergencyContact = _emergencyContactService.getEmergencyContact(contact); emergencyContactService.setEmergencyContact(emergencyContact,form); if(! contact.hasEmergencyContact()){ contact.setEmergencyContact(emergencyContact); emergencyContact.addarrContacts(contact); } _emergencyContactService.save(emergencyContact); } // Repeat this logic to check if work object, travel object or address object have been selected in the form also, get and set accordingly. </code></pre> <p>I then repeat the if statement shown above for emergency contact, to check whether the work object, travel object or address object have been selected in the form also. If so, get and set the object, then link the contact object where necessary.</p> <p>This doesnt seem efficient to me, especially as I need to repeat all this logic on another controller method as well. Is there a better way to structure my objects, or a better way to handle my controller code?</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload