Mach II event round trip
This is a post I just made on the Mach II google group list. Someone there is looking at MachBlog code to learn about Mach II. He had a question about calling the getUserByID() function and how it gets the data from the database.
-From the admin, when you are viewing the list of User Accounts, you
have the [edit] link by each name. Clicking that calls the
showUserForm event. To see what will happen with that event, view the
mach-ii.xml file.
- After the security filter and a qForm flag is set, the getUserByID
method in the userListener is called.
- That takes the userID which was part of the [edit] link and
subsequently put into the Mach II event object (the framework does
this behind the scenes) and passes it to the getUserByID method in the
UserService.
- The getUserByID method in UserService first creates a new instance
of the User bean and calls the init() method on it, passing in the
userID. This new instance now has that userID that originally came
from the [edit] link. So the bean, which represents a single record in
the table, has the ID, but no other data from the table.
- We now pass that bean into the read() method of the UserDAO. It will
use that bean to simply pull the userID out so it can read the record
from the machblog_user table.
- After the query is run, we populate the rest of the user bean with
the data from the query. This is done with <cfset
arguments.user.init(.... long list of variables ....) />.
- Now, nothing is returned by the read method. That's where "pass
by reference" takes effect. The instance of the User
bean that was created in UserService is the same instance that we just
populated with the query values.
- Going back to the UserService, you see that we do return the user
bean. That goes back to the UserListener which in turn returns it to
the M2 framework.
- Back in the mach-ii.xml file, you'll see that the <notify> line has
resultArg="user". That means that the framework will take whatever is
returned from the getUserByID method will be put into the event object
with a name of "user".
- So then in the view page for showing the user form
(views\admin\userForm.cfm), we can get a reference to that with this
line of code:
<cfset variables.user = event.getArg("user") />
- Now in the view (for additional code go to the skins folder) you can
get to the data that the bean has (which came from the machblog_user
table) by making calls to variables.user.getUserID(),
variables.user.getFirstName(), etc.
Kind of a long post, but that is a Mach II round trip for getting the
record from the database given a userID. There is a bit more going on
behind the scenes with ColdSpring and all, but that can be ignored for
this basic scenario.
1
Comment
|
Mach II
|
Send
Posted
2/15/08
@ 9:23 AM
by Matt Williams
| Comments |
Just an FYI, I have two very detailed posts covering the inner workings of Mach-II. Similar to this post I have an examination of the request process here: http://www.remotesynthesis.com/blog/index.cfm/2007/9/27/Anatomy-of-a-Framework--MachII--Part-2--The-Request-Process and the application startup process here: http://www.remotesynthesis.com/blog/index.cfm/2007/8/9/Anatomy-of-a-Framework--MachII--Part-I |
| Posted by Brian Rinaldi @ 2/15/08 9:51 AM |






