Performing multiple steps using ORM, Services, and ColdBox

Hey guys,

I really value this group’s input, especially on the subject of application structure and maintainability. Here’s something I’ve run into lately:

I’ve been working on converting a fairly large project of spaghetti code over to Coldbox using persistent CFCs for ORM and virtual entity services for my services. Consider the files below…

model/Order/Order.cfc
model/Order/OrderService.cfc

model/Order/OrderLine.cfc
model/Order/OrderLineDecoration.cfc

In our application, one order can have many lines, and each line can have many decorations. I’ve defined the one-to-many relationships in each of my CFCs.

Let’s say I have a shopping cart system where a user can select their items, decorate them, and after finalizing their order, the system would create the order, add the order lines, and also add any decoration to each line.

My first question is - Is this something that would ideally be placed in a single function in my OrderService.cfc? Something like a function called createOrder() that performs all of these steps I mentioned about?

My next question is - is their some sort of ORM magic or ColdBox magic that would make this super easy? Maybe where after passing in the data, all of this could be persisted with something like order.save() or ormService.save(order).

Thanks for all the help.

Best,

Grant

Grant,

In question 1 there is no hard or fast rule, but yes you can do that. Question 2 yes the order.save() would persist the relationships as well.

I have an ordering system with one less level (no decoration). I have one method in my OrderService that handles the creation of an order. In my system, since orders aren’t anything without items, I manage the creation of both at the same time. Something like:

// populate the order
var order = OrderService.populate( memento=somestruct );
for( item in items ) {
var orderitem = OrderItemService.populate( memento=someotherstruct );
order.addItem( orderitem)
// add extra layer for decorations

}
OrderService.save( order );

Thanks guys!

Joel, do you have to do anything special in your persistent CFC’s (ORM) to make this work? Like insert=“true” or something?

// One to many
property name=“lines” fieldtype=“one-to-many” cfc=“Order.OrderLine” insert=“true” update=“true” lazy=“true”;

Best,

Grant

You’ll need to add the fkcolumn that describes the relationship between Order and OrderLine.

property name=“lines” fieldtype=“one-to-many” cfc=“Order.OrderLine” fkcolumn=“OrderID {or whatever the relationship column is}” …

One more thing. Is order.addItem( orderitem) a function you yourself write, or is that some magic included with Coldbox or ORM that I don’t know about?

addItem() would be automatically generated based on the configuration of the property. And actually, with the way you currently have it, it would be addItems() {plural}. If you want the singular (addItem()), you could specify the singularName property.

When dealing with ORM and relationships certain methods are automatically given to you, for example addItems(), hasItems() and many others. The same as normal properties have getters/setters.

All of this is well documented in the CF-ORM of the ColdFusion documentation. If you are new to CF-ORM then it might be advisable to read up on all those before getting serioulsy stuck in ColdBox and the many ORM services it offers.

Wow, that is awesome! I’ve been writing spaghetti code for a long time and structuring the model layer of my applications has always been the part I’ve struggled with most. Before, if I wanted to display all the order information on a single page, I would have to do multiple queries by hand, queries within loops at times, it was a nightmare!! It is a nightmare! Now after learning some Coldbox and ORM, I actually became sick to my stomach when I learned that I could pull down all order data with one line of code in my controller.

This is going to make my life at work so much better. Thanks for the help!

Andrew,

That’s good advice. I’ve only skimmed the docs so far.

No problem, and feel free to still ask if you’re not sure of anything and need clarification.

It won’t be long before you think how did I get so much done in the past?

Once you go ORM (and especially CB-Virtual Entity Services), it’s incredibly difficult to think about doing things differently :slight_smile: