I decided to not wait till late night for my next Transfer post and instead write during lunch. Young and the Restless is a good one today so hopefully I won't get distracted!
First off - an important point to make about my last post. I did not actually walk you through creating a DSN or setting up the database. I did talk about the datasource.xml file, but I didn't actually tell folks to go ahead and make the database. This may have led to bugs when folks tried to run the code. Fred posted a quick SQL script that would work well and I'll include a script in the this entries zip.
To be 100% crystal clear: Transfer does not automatically create your tables for you. This is something you will need to do. In my past work with Transfer (for Broadchoice), I typically began by editing the Transfer.xml file first to define the properties for my object, and then I went to the table. One of the real nice things about Hibernate is that it does all of this for you. You can literally drop the entire set of tables and Hibernate will have no problem recreating it for you. Shoot, you can even add a property in your model and Hibernate will handle adding the column. Transfer doesn't do this - but it certainly does do a lot, so don't take this as "Transfer Sucks!" view. I wouldn't be writing about Transfer if I didn't find it both cool and pretty darn practical. So with that said, and with your database setup (again, I've included the SQL file in the zip), you can now begin doing CRUD operations within Transfer. CRUD is a shorthand way of saying Create (make a new thing), Read (get a persisted thing), Update (make changes and persist the thing), and Delete (send the thing to the Forbidden Zone). The Transfer component we made yesterday has methods for all of these. Let's start simple and look at how we can make an Employee. The new method takes the name of a class (what we defined in Transfer.xml) and creates an object from it. Here is an example:- No need to write any SQL
- No need to worry about the database table. If for some reason a column name changed, we can handle it in the XML
- No need to make a 'bean' CFC.
The transfer_* properties are obviously specific to Transfer. You can use another debug function, getPropertyMemento(), to return only the properties that relate to your object.
So cool, we got a nice little object. Let's set some data in it, just for the heck of it.
2 <cfset e.setLastName("Newman")>
3 <cfset e.setDOB(createDate(1973,4,8))>
4 <cfset e.setEmail("vnewman@newmanenterprizes.com")>
5 <cfset e.setPhone("555-555-5555")>
2 <cfset e.setFirstName("Victor")>
3 <cfset e.setLastName("Newman")>
4 <cfset e.setDOB(createDate(1973,4,8))>
5 <cfset e.setEmail("vnewman@newmanenterprizes.com")>
6 <cfset e.setPhone("555-555-5555")>
7 <cfdump var="#e.getMemento()#">
8 <cfset application.transfer.save(e)>
9 <cfdump var="#e.getMemento()#">
Woot! We are halfway through our CRUD. Let's look at read. There are actually multiple ways of getting data via Transfer. You can get lists of objects (as a query) or a single object. This can be done via searches or by getting a particular object by id. For today's blog entry we will focus on simply loading an object based on an ID. As you can probably guess, there is another method in Transfer we will use: get
The get method takes a class name and a ID value. Here is a simple example.
2 <cfdump var="#e2.getMemento()#">
2 <cfset application.transfer.delete(employee)>
Comment 1 written by Tim Garver on 5 November 2008, at 1:31 PM
Great job, I am working right along with you.
I want to see how you get the list or query object without having to setup a gateway object, i am still looking for this one. I will keep trying, but so far I had to call my gateway to get it.
Keep up the good work
Tim
Comment 2 written by Yves on 5 November 2008, at 5:07 PM
The thing that came to mind was about the same thing that Tim just mentioned... getting lists or something similar without a gateway object.
;-)
Comment 3 written by Raymond Camden on 5 November 2008, at 5:12 PM
Then again - maybe I'm teasing. ;)
Comment 4 written by Yves on 5 November 2008, at 5:57 PM
;-)
Can't wait to read the follow up!
Keep up the great work(s)!