the singularity of being and nothingness
Posts tagged ORM
Criteria Builder Quickie: Disjunction
Mar 12th
If you use ColdBox’s awesome CriteriaBuilder at all, you know it’s dead simple to create complex AND dynamic criteria queries. If you’re like me, a lot of your criteria queries are a bunch of “and”s built together. For example, let’s imagine we’re trying to get all the cars from our database that have a make of Ford and a color of Red. A simple criteria query for this might look like:
// create criteria var c = newCriteria(); // add criteria c.isEq( "Make", "Ford" ) .isEq( "Color", "Red" ); // get result var result = c.list();
Pretty simple, right?
Of course, not all queries are “and“s. Sometimes, you need to do an “or“. Perhaps we want to get all the cars that are Ford OR Red. Again, this is very easy with CriteriaBuilder:
// create criteria var c = newCriteria(); // add criteria c.or( c.restrictions.isEq( "Make", "Ford" ), c.restrictions.isEq( "Color", "Red" ) ); // get result var result = c.list();
While this works, it’s not complete. Sure, there are some scenarios where you will know ahead of time what criteria you want to use in a criteria query. However, consider a scenario in which you have a search form in which a user can select from a list More >
ColdFusion ORM – Collection…was not processed by flush()
Nov 14th
This is more for my own memory, but perhaps it will be useful to others 🙂
Let’s say I have the following entities:
User.cfc
component persistent="true" entityname="User" table="User" { property name="UserID" fieldtype="id"; property name="Name"; property name="Age"; property name="Group" fieldtype="many-to-one" cfc="Group" fkcolumn="GroupID"; }
 Group.cfc
component persistent="true" entityname="Group" table="Group" { property name="GroupID" fieldtype="id"; property name="Name"; property name="Activities" fieldtype="one-to-many" fkcolumn="GroupID" linktable="Activity" inversejoincolumn="ActivityID"; }
Ok, so let’s also imagine that I’m trying to leverage the postDelete() event to make an audit record of any User deletions that I make:
// function postDelete( entity ) { // get properties from entity that I want to log ... ... }
When I try to do this, I get something like:
collection >> [Group.Activities] was not processed by flush()
I banged my head on this wall for a while, until I finally figured out the issue.
The issue is the “laziness” of the “Group” property on the User entity. Since I have no value specified, it is equivalent to having specified “lazy=true”. Since (I assume) the Group property is not being fully “loaded” during the delete operation, there’s no chance that it’s own “Activities” property is loaded either…hence the error.
Anyway, to fix this, I merely added “lazy=false” to my “Group” property. Once in place, I was able to More >
Custom SQL Projections in CriteriaBuilder
Aug 25th
In my last post, I showed how you can pretty easily integrate ColdBox’s awesome CriteriaBuilder with subqueries. As I was using this approach, I came across another (unrelated) challenge that I wanted a solution for.
Simple Grouping/SummingIn one of my queries, I wanted to do some simple grouping/summing. With CriteriaBuilder, you can do this very easily by adding a projection or two.
For example, let’s imagine that we have the same entities as before:
- Car
- Driver
- Insurance
This time, we want to get a sum of the total number of cars, grouped by “make”. Easy:
public struct function getCarsByMake() { var c = newCriteria(); c.withProjections( count="CarID", groupProperty="Make" ); var results = { data = c.list() }; results.count = arrayLen( results.data ); return results; }
And the SQL:
select this_.Make as y0_, count(this_.CarID) as y1_ from Car this_ group by this_.Make
Pretty simple. By the application of the “count” and “groupProperty” projections, we get back a nice array of the grouped data. Note, of course, that because of the application of projections, our result set is no longer in the domain of discreet entities. Rather, since we asked for aggregate data, that’s what we get! Of course, this means that results.count has to be calculated differently. Instead of running CriteriaBuilder’s count() method More >
ColdFusion Bug? EntityToQuery() and Unknown Data Type
May 2nd
I ran into an interesting issue today. To make a long story a little less uninteresting, I was basically trying to go from an array of ORM entities to an Excel dump of the data in those entities. Obviously, the easiest way to accomplish this is to first convert the array of entities to a query, and then pass the query result set off to the built-in CF Excel manipulation functions.
Easy enough. With one little line of code, you can very simply convert ORM entities to a ColdFusion query…just like so:
// get the orm entities ormstuff = EntityLoad( "Art" ); // convert the orm goodness to a CF query anicequery = EntityToQuery( ormstuff );
Cake, right? If I were to hand this query off to CF’s spreadsheet functions, I could easily prepare and export a nice Excel file full of the data I just converted. But of course, nothing is that simple…and that’s where the potential bug announced itself.
A Dark ForebodingYou see, instead of simply dumping all the data for all the columns of the query-which-was-once-an-entity, I wanted to pare down the list of columns that would be output in the Excel export. Since I already had a query handy, More >
Update Miscellany
Mar 29th
Wow, it's been far too long since I've posted. I've been quite busy as of late, and have been putting the finishing touches on a couple of pretty decent sized projects.
But perhaps most interesting to followers of this site is the fact that I'll be working with a friend of mine over the next couple of months to completely revamp a site that we think will be pretty sweet in a couple of months. But the coolest part is that we're going to be creating this site–as much as possible–using exclusively ColdFusion ORM. As we make progress, I'm going to do my best to regularly blog about our experiences as we tap into this extremely cool technology that is available in CF9.
So anyway, stay tuned…I hope to have some killer posts up very soon 🙂
Share this:ColdFusion and ORM…Yummy!
Mar 2nd
The other day, I was flipping through the ColdFusion docs (yeah, super exciting!) and came across the section about ColdFusion's support of ORM. If you're not familiar with it (I wasn't really until a few days ago), ORM stands for "object-relational mapping." While it sounds a bit overly technical, ORM is basically a way to interface objects in code to relational databases that levels the playing field, allowing your code to be more or less agnostic about what kind (or kinds) of databases that it's connecting to.
At first, I was unimpressed, but then I took a few moments to think about it. How often have I had to switch between database systems? Plenty. And how fun was it to have to go back and rework my code to account for the differences in typing and syntax? It wasn't. With ORM, however, alot of these headaches are removed because you can create an abstraction layer in your application using ORM to not really have to worry about what datasource you might be connecting to.
Now obviously, the subject of ORM is much more complex than my admittedly weak description. However, after a lot of reading I feel like I have a decent More >