Simple create in ColdBox using ORM CF9

In my last post I showed you how to set-up ColdBox with ORM. I also showed you how to use your service layer within your handlers. Today, an example of how to create a new record in ColdBox using ORM.

Let's use a real pretend example for this. Fake Jake has the following issue he wishes to address.

Jake wants to create a simple sign-up form on his website. He is already using Coldbox M6 and wants to use ORM to create a database with user information and populate it with a simple sign-up form. He has already followed the example post on how to set-up ORM within CB.

Well Jake, first you will need to create the Domain Object for the user sign-up. Nothing complicated about this only a few fields will be needed. However, I have reminded Jake to set-up the event handler correctly within in his Application.cfc. This is done by adding the ORMEventHandler.cfc to the model folder and making sure this 'eventhandler' attribute is 'ORMEventHandler.cfc'

view plain print about
1// ORM Setup
2 this.ormEnabled = true;
3 this.datasource = "myDS";
4 this.ormSettings = {
5 dbcreate = "update",
6 dialect = "MicrosoftSQLServer",//Specifies the dialect.
7 logSQL = true,
8 eventhandling = true,
9 eventhandler = "model.ORMEventHandler", <----------here it is
10 flushAtRequestEnd = false
11 };

When Jake has done this he will be ready to create the Domain Object. I know Jake only needs to collect a few details from his users, user name and email address. Jake will create a new object within his model folder called user.cfc to achieve this.

view plain print about
1component output="false" persistent="true" {
2property name="user_name" column="user_name" type="string"ormtype="string";
3property name="user_email" column="user_email" type="string";
4 users function init() output=false{
5 return this;
6 }
7
8}

As you can see, it's super simple. If Jake was to run the code for the first time he will see the user table had been created.

Reply from Jake:

"Wow my table user.cfc has been created in my database"

I didn't know Jake could reply back! But as he did I see one mistake in the domain object. He will need to add a primary key, something to uniquely identify each user when he wants to get or update them later on. Try this...

view plain print about
1component output="false" persistent="true" {
2property name="user_id" column="user_id" type="numeric" ormtype="int"
3fieldtype="id" generator="increment";
4property name="user_name" column="user_name" type="string" ormtype="string";
5property name="user_email" column="user_email" type="string";
6 users function init() output=false{
7 return this;
8 }
9
10}

Simple enough for Jake needs, however he will need to reload ORM to update the table. He can do this by appending ORMReload=1 to his URL scope. Because Jake has set his 'dbcreate' attribute to update in his Appication.cfc no current data will be lost. This is a mistake I cough, cough, Jake will only ever make once!

This gives Jake an object, he will need a 'view' in Coldbox to populate his entity (I will show you how to create a new entity in just a second). As Jake already knows how to set-up views in ColdBox (I asked him, really, he can reply back now!), so his view will look something like this.

view plain print about
1<form action="#event.buildLink('user.save')#" method="post">
2<!--- name --->
3<label for="user_name" class="required">Your Name: </label>
4<input type="text" id="user_name" name="user_name" value="">
5
6<!--- email --->
7<label for="user_email" class="required">Email: </label>
8<input type="text" id="user_email" name="user_email" value="">
9    
10<input type="submit" class="subBut" value="Signup">

Next he will need to post to a handler that saves his new user in ColdBox. I recommend he does the following things within this handler...

  • Get the URL scope
  • Create a new entity
  • Save new user

Above can be done in just 6 lines of code! Jake has already injected his models correctly and the service is available within his handler to use so below will do the tick.

view plain print about
1function save(event){
2var rc = event.getCollection();
3var userBean = userService.new(ENTITYNAME="users");//new
4event.removeValue("user_id");
5populateModel(model=userBean);
6transaction {entitySave(userBean);}
7}

Easy isn't it Jake? " " – no answer this time, but I am sure Jake agrees with me on this. A lot is happening in just these 6 lines of code, let me explain....

Line 2: ColdBox gets the request scopes.

Line 3: Creates a new entity using Jakes userService and puts it into the rc scope. This creates an empty object ready for population. Important "var" it!!!! Another mistake I, sorry, I mean Jake will only make once!

Line 4: Uses the built in ColdBox plugin to update an object properties via its setters automatically from the RC scope (forms fields etc) as long as they are named the same that is!

Line 5: Saves Jakes new user to the database.

Done! Fake Jake, I hope this walk through will help. I have a feeling your next question is about validation and updating, right? "NOTHING!" O well.

Related Blog Entries

Oct03

Comments 0