Understanding and Using CFCs (with OOP and database interaction)
Part 3
Writing an method to insert data
The first database interaction method we will create is a insert method. This method will take data that has been assigned our components properties and insert them into the database.
After we insert the data into the database, we will then populate the id property we defined, since we will then have a unique id number from the database.
<cffunction name="insertJob">
<!--- insert job data into the database --->
<cfquery datasource="#request.dsn#" name="qInsertJob">
set nocount on;
insert into jobs( display_name )
values ( '#this.name#' );
select @@identity as job_id
</cfquery>
<!--- now set the id property of this component
instance to the identity of the field we just inserted--->
<cfset this.id = qInsertJob.job_id>
</cffunction>
This function code can be placed anywhere inside our <cfcomponent>.
Our component so far
Just to be sure all of our pieces are correctly in place, here is the complete code to our job.cfc component file so far:
<cfcomponent
displayname="Job Component"
hint="This component handles all job functions and database calls">
<cfscript>
// call our internal initialize function
init();
</cfscript><cffunction name="init">
<!--- initialize data for component --->
<cfparam name="this.id" default="">
<cfparam name="this.name" default="">
</cffunction>
<cffunction name="insertJob">
<!--- insert job data into the database --->
<cfquery datasource="#request.dsn#" name="qInsertJob">
set nocount on;
insert into jobs( display_name )
values ( '#this.name#' );
select @@identity as job_id
</cfquery>
<!--- now set the id property of this component
instance to the identity of the field we just inserted--->
<cfset this.id = qInsertJob.job_id>
</cffunction>
</cfcomponent>
Lets test our component and see it in action
Now that we have our component able to insert data into the database, lets create a simple form inside a .cfm file for creating jobs using our component.
Well create the following file and save it as jobs_create.cfm. This file is just a simple input form to take a name for the new job for creation:
<html><head><title>Create Job Form</title></head>
<body>
Create a new job<br>
<form action="job_create_action.cfm" method="post">
job name : <input type="text" name="name">
<br><br>
<input type="submit" value="create job">
</form>
</body>
</html>
Obviously, we are sparing any fancy formating to focus soley on functionality.
Now we will need to create our action page that jobs_create.cfm submits to. In this page we will call our component to create our record in the database.
We will call this file job_create_action.cfm:
<cfscript>
// create a job componet object
newJob = createObject("component", "job");
// now set the properties of if from our form
newJob.name = form.name;
// now insert job into the database
newJob.insertJob();
</cfscript>
<!--- now that we've inserted our data, lets output the component
to the screen and see what it looks like. --->
<cfdump var="#newJob#">
Lets analyze the above code line by line and inspect what is happening.
newJob = createObject("component", "job");
The first line creates a new job component with the createObject() function. This function takes two arguments. The first is the type of object, in this case it is of course "component". The second argument is the name of the object you want to create. This will be the name of our CFC file, without the .cfc extension, "job".
When this code is executed, the variable newJob will hold an instance of our job CFC. It is at this point also, when our component is created, that our init() function is executed.
newJob.name = form.name;
Now we are assigning one of the properties of our component, name, to a value. Here specifically we are setting it to the form field that was submitted.
newJob.insertJob();
This line executes our method to insert the data into the database. Since each instance of our component object has all of the methods and properties, the method is automatically available. All we need to do is simply call it!
Lets take a look at the generated output after the insert
If you've coded everything exact so far, a record will be inserted into the database, followed by a <cfdump> output of our component object. Your displayed output will look similar to this (with some additional formatting of course):
| component job | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| NAME | My test job | |||||||||||||
| ID | 1028911 | |||||||||||||
| INIT |
| |||||||||||||
| INSERTJOB |
| |||||||||||||
In the next part we will create a method to search our job records and display them to the screen via our component.