All pages
Powered by GitBook
1 of 4

Loading...

Loading...

Loading...

Loading...

Scaffold

Now let us relish in the power of the orm-crud command:

coldbox create orm-crud entity=models.Contacts pluralName=Contacts

That's it.

Generated Handler: /Users/lmajano/tmp/restapp/handlers/Contacts.cfc
Generated View: /Users/lmajano/tmp/restapp/views/Contacts/edit.cfm
Generated View: /Users/lmajano/tmp/restapp/views/Contacts/editor.cfm
Generated View: /Users/lmajano/tmp/restapp/views/Contacts/new.cfm
Generated View: /Users/lmajano/tmp/restapp/views/Contacts/index.cfm

The command will create the handler with RESTFul capabilities, the views and all the necessary machinery for you to manage Contacts! Now go get a nice latte in celebration!

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: ORM Scaffolding

Now let's use the power of ORM and CommandBox to scaffold everything for you :). The help for this command is here:

coldbox create orm-crud help

It even creates all the integration tests for you.

Contacts.cfc

Let's use CommandBox to build it:

coldbox create orm-entity entityName=contacts 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"}
    };

}