What the CRUD?!
CRUD (Create, Retrieve, Update, Delete)
When interacting with any form of storage, these are the 4 basic methods that
you will use. Any time you are dealing with files, you will mostly only Create
a file, Retrieve a file, Update a file, or Delete a file. When dealing with
data, you will mostly only Insert (Create), Retrieve (Select), Update (Update),
or Delete (Delete) a record or groups of records within your database. CRUD
is essentially just an acronym that helps you to organize your code when interfacing
with storage. It can also help when dealing with objects within your code. Consider
a User object in a membership management application as an example. When thinking
in terms of CRUD, we can see that these 4 same actions will be applied to any
User object. You will either Create, Retrieve, Update, or Delete the user. While
you may have other methods within your User object, these are going to be the
primary methods you will use to interact with a User (or most any object for
that matter), and any other method is probably going to use one of these 4.
So what’s the point? Well, when you are building an application and working
with objects, if you already know that you will most likely need to perform
these 4 methods on any of these objects… you can be sure to include them in
your CFC on the front end. Using CRUD also gets you to think more in terms of
… ‘what am i going to do with this object?’:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <cfcomponent hint="this is a user object"> <cffunction name="create"> <cfargument name="firstname" default="" required="true"> <cfargument name="lastname" default="" required="true"> <cfargument name="username" default="" required="true"> <cfargument name="password" default="" required="true"> <cfargument name="email" default="" required="true"> <cfargument name="dsn" default="" required="true"> <cfquery name="usersQuery" datasource="#argument.dsn#"> Insert Into Users (firstname, lastname, username, password) Values ('#arguments.firstname#', '#arguments.lastname#', '#arguments.username#', '#arguments.password#') </cfquery> </cffunction> <cffunction name="retrieve"> <cfargument name="id" default="" required="false"> <cfargument name="dsn" default="" required="true"> <cfquery name="usersQuery" datasource="#arguments.dsn#"> Select * from users <cfif #arguments.id# GT ""> Where user_id = #arguments.id# </cfif> </cfquery> <cfreturn usersQuery /> </cffunction> <cffunction name="update"> <cfargument name="firstname" default=""> <cfargument name="lastname" default=""> <cfargument name="username" default=""> <cfargument name="password" default=""> <cfargument name="email" default=""> <cfargument name="dsn" default="" required="true"> <cfargument name="id" default="" required="true"> <cfquery name="usersQuery" datasource="#arguments.dsn#"> Update Users Set firstname = '#arguments.firstname#', lastname = '#arguments.lastname#', username = '#arguments.username#', password = '#arguments.password#', WHERE user_id = #arguments.id# </cfquery> </cffunction> <cffunction name="delete"> <cfargument name="id" default="" required="true"> <cfquery name="usersQuery" datasource="#arguments.dsn#"> Delete from users Where user_id = #arguments.id# </cfquery> </cffunction> </cfcomponent> |
now that your user CFC is built… you can instantiate it from anywhere in your
application for the available methods… let’s say a user has submitted a ‘join
now’ form on your site, and you want to create a record of them in the database:
1 2 3 4 | <cfscript> user = createObject("component", "user"); createUser = user.create(firstname: "#form.firstname#", lastname: "#form.lastname#", username: "#form.username#", password: "#form.password#", email: "#form.email#", dsn: "#application.dsn#"); </cfscript> |
Now, let’s say that users are created from many different places in your website,
perhaps through a ‘join now’ form, and maybe from a back-end admin, possibly
you have a reseller admin where resellers are allowed to create users, and you
have an API that you also offer to external sites so that they are also able
to create users… that is 4 points within your application where users can
be created. Let’s say that you decide that you want new users to be emailed
a welcome email with thier login information. If you were not using CFCs with
a CREATE method… you would need to sift through your code at each of these
4 points of entry in order to add your notification script. Using CFCs and a
CREATE method, you would only need to make the change in one place:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <cffunction name="create"> <cfargument name="firstname" default="" required="true"> <cfargument name="lastname" default="" required="true"> <cfargument name="username" default="" required="true"> <cfargument name="password" default="" required="true"> <cfargument name="email" default="" required="true"> <cfargument name="dsn" default="" required="true"> <cfquery name="usersQuery" datasource="#argument.dsn#"> Insert Into Users (firstname, lastname, username, password) Values ('#arguments.firstname#', '#arguments.lastname#', '#arguments.username#', '#arguments.password#') </cfquery> <!--- let's welcome the user, and give them thier login information ---> <cfmail to="#arguments.email#" from="#application.adminEmail#" subject="Welcome to our site!"> Welcome to our site #arguments.firstname# #arguments.lastname#!<br> Here are your login details:<br> <br> username: #arguments.username#<br> password: #arguments.password#<br> <br> -staff </cfmail> <!--- let's also notify the admin ---> <cfmail to="#application.adminEmail#" from="#application.adminEmail#" subject="New Site User!"> There is a new user our site: #arguments.firstname# #arguments.lastname#! </cfmail> </cffunction> |
more appropriately, you should have a notification.cfc that accepts arguments
like “to”, “subject”, and “message” … and then just invoke that notification.cfc
from within the user.create method… but for sake of clarity, we’ll leave it as is
for now.






















Wikinomics: How Mass Collaboration Changes Everything
Adobe Flex 3: Training from the Source
Breaking Out of the Web Browser with Adobe AIR
August 2nd, 2007 at 1:00 am
[...] What the CRUD?! [...]