[coldbox-3.8.1] Confusion on approaches for communicating with database - populateFromQuery

Thanks in advance folks, I’m new to both ColdFusion and ColdBox and really trying to grapple with this whole framework ecosystem.

I have about a hundred questions but we’ll untangle this knot in my brain one at a time :slight_smile:

How/when/why might you appropriately use populateFromQuery?

I’ve tried this:

`

prc.clientQuery = new Query( sql = ‘SELECT TOP 100 * FROM myDB.Client’);
prc.clientObjects = populator.populateFromQuery(target=getModel(“Client”),qry=prc.clientQuery);

`

But I get this error: The QRY argument passed to the populateFromQuery function is not of type query.

However when I execute a writeDump(prc.clientQuery) I see that it is of type component com.adobe.coldfusion.query …what gives? I know I’ve gone off the rails here somewhere…

You’ll ask, “What are you trying to do?” and I don’t really have a good answer…I am that lost. In my mind I think that I can query the database directly with native SQL, then somehow load those returned results into an array of objects I can loop through…I guess. I don’t need to do this, just trying to understand what my options are before I need to do something.

I’m used to just building up SQL code in PHP, sending to the database, getting results back as an array and looping. I am trying to learn ORM and Coldbox Criteria Builder as part of this endeavor but at times for simplicity I can see the usefulness of just executing raw SQL and working with the results.

Thanks for helping me understand this powerful framework.

Wes

You’re almost there. What you have is an object that represents the SQL you’d like to run-- but not the actual results. In other words, nothing has actually been sent to the database server yet.

All you need to do is call the .execute() method on your query object. NOW when you dump out the results of that method you should see the actual data-- represented as a sort of array of structs if you will.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSe9cbe5cf462523a0693d5dae123bcd28f6d-7ffb.html

That question actually had nothing to do with ColdBox per se, but we’re happy to have you here. If you haven’t seen it, here are a series of general ref cards for getting started with the ColdBox platform:

http://wiki.coldbox.org/wiki/Dashboard.cfm#PDF_Ref_Cards

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Brad thank you for such a fantastic and fast response -- that works great!

So from there I can work with that array of results, which is wonderful -- but is there a way to identify/associate them as Client objects from my Model? So, for example if I have have functions as part of my model as methods of the Client object can I somehow construct or reconstitute this array of data as actual Client objects with properties and methods? Is that what populateFromQuery does? Sorry if this is a ridiculous question!

I think one of my fundamental misunderstandings is how, or if, there is any interplay between executing raw queries and ORM-related techniques. Could you use one method for a few lines then swith back into ORM? I realize raw SQL is communicating with the persistence layer directly while ORM-related techniques leave it to the model to handle persistence. It just seems like there are so many ways to work with the data within Coldbox (SQL, HQL, ORM, Criteria Builder...) I just can't seem to wrap my brain around which approach is best suited to certain occasions.

Thanks for your help so far and any additional advice you have!

Wes

After watching the following two videos I have a bit more clarity: