All pages
Powered by GitBook
1 of 5

Loading...

Loading...

Loading...

Loading...

Loading...

Views

Here are the views as well, isn't it awesome that the views stay the same :), that means we did a good job abstracting our model and controllers.

index.cfm

<cfoutput>
<h1>Contacts</h1>

<cfif flash.exists( "notice" )>
    <div class="alert alert-info">#flash.get( "notice" )#</div>
</cfif>
<cfif flash.exists( "errors" )>
    <div class="alert alert-danger">#flash.get( "errors" ).toString()#</div>
</cfif>

#html.href(href='contacts.editor',text="Create Contact")#
<br><br>
<cfloop array="#prc.contacts#" index="contact">
<div>
    #contact.getLastName()#, #contact.getFirstName()# (#contact.getEmail()#)<br/>
    #html.href(href='contacts.editor.id.#contact.getContactID()#',text="[ Edit ]")#
    #html.href(href='contacts.delete.id.#contact.getContactID()#',text="[ Delete ]",onclick="return confirm('Really Delete?')")#
    <hr>
</div>
</cfloop>
</cfoutput>

editor.cfm

Check out our awesome html helper object. It can even build the entire forms according to the Active Entity object and bind the values for you!

<cfoutput>
<h1>Contact Editor</h1>

<cfif flash.exists( "notice" )>
    <div class="alert alert-info">#flash.get( "notice" )#</div>
</cfif>
<cfif flash.exists( "errors" )>
    <div class="alert alert-danger">#flash.get( "errors" ).toString()#</div>
</cfif>

#html.startForm(action="contacts.save")#
    #html.entityFields(entity=prc.contact,fieldwrapper="div")#
    #html.submitButton()# or #html.href(href="contacts",text="Cancel")#
#html.endForm()#
</cfoutput>

ORM

You will first make sure your contacts datsource exists in the Administrator and then we can declare our ORM settings in our Application.cfc

// ORM Settings
this.ormEnabled       = true;
this.datasource          = "contacts";
this.ormSettings      = {
    cfclocation = "models",
    dbcreate    = "update",
    logSQL         = true,
    flushAtRequestEnd = false,
    autoManageSession = false,
    eventHandling       =  true,
    eventHandler      = "cborm.models.EventHandler"
};

These are the vanilla settings for using the ORM with ColdBox. Make sure that flushAtRequestEnd and autoManageSession are set to false as the ORM extensions will manage that for you.

In this example, we also use dbcreate="update" as we want ColdFusion ORM to build the database for us which allows us to concentrate on the domain problem at hand and not persistence. You also see that we add our own eventHandler which points to the extension's event handler so we can make Active Entity become well, Active!

Activating ORM injections

Now open your ColdBox.cfc and add the following to activate ORM injections inside of your configure() method.

orm = { injection = { enabled=true } };

Coding: Virtual Service Layer

Now let's build the same thing but using ColdFusion ORM and our Virtual Service Layer approach, in which we will use a service layer but virtually built by ColdBox. This will most likely give you 80% of what you would ever need, but in case you need to create your own and customize, then you would build a service object that extends or virtual or base layer.

+ handlers 
  + contacts.cfc
+ models
  + Contact.cfc

Contacts Handler

That's right, go to the handler now, no need of data layers or services, we build them for you!

coldbox create handler name=contacts actions=index,editor,delete,save

Now spice it up

/**
* I am a new handler
*/
component{

    // Inject a virtual service layer binded to the contact entity
    property name="contactService" inject="entityService:Contact";

    function index(event,rc,prc){
        prc.contacts = contactService.list(sortOrder="lastName",asQuery=false);
        event.setView("contacts/index");
    }

    function editor(event,rc,prc){
        event.paramValue("id",0);
        prc.contact = contactService.get( rc.id );
        event.setView("contacts/editor");
    }

    function delete(event,rc,prc){
        event.paramValue("id",0);
        contactService.deleteByID( rc.id );
        flash.put( "notice", "Contact Removed!" );
        setNextEvent("contacts");
    }

    function save(event,rc,prc){
        event.paramValue("id",0);
        var contact = populateModel( contactService.get( rc.id ) );
        var vResults = validateModel( contact );
        if( !vResults.hasErrors() ){
            contactService.save( contact );
            flash.put( "notice", "Contact Saved!" );
            setNextEvent("contacts");
        }
        else{
            flash.put( "errors", vResults.getAllErrors() );
            return editor(event,rc,prc);
        }
    }

}

Contacts.cfc

Let's use CommandBox to build it:

coldbox create orm-entity entityName=contact primaryKey=contactID properties=firstName,lastName,email --open

Then spice it up with the validation constraints

/**
* A cool Contact entity
*/
component persistent="true" table="contacts"{

    // Primary Key
    property name="contactID" fieldtype="id" column="contactID" generator="native" setter="false";

    // Properties
    property name="firstName" ormtype="string";
    property name="lastName" ormtype="string";
    property name="email" ormtype="string";

    // validation
    this.constraints = {
        firstName = {required=true},
        lastName = {required=true},
        email = {required=true, type="email"}
    };

}