Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
ColdFusion 9 Developer Tutorial
ColdFusion 9 Developer Tutorial

ColdFusion 9 Developer Tutorial: Create robust professional web applications with ColdFusion

eBook
$28.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

ColdFusion 9 Developer Tutorial

Chapter 2. Basic CFCs and Database Interaction

All of the major software platforms have different forms of objects. In ColdFusion, these are called ColdFusion Components (CFCs). CFCs let us package up and reuse code in ways that will make our development much easier. Our approach in this book will be to learn by doing. In this chapter, we will learn by building the start of a simple shopping system. We will also learn to work with databases. Let's start learning the easier way to write software with CFCs. The following is a list of the contents that will be covered in this chapter:

  • The ColdFusion object called CFC

  • Making objects/CFCs come alive with methods

  • The difference between a class and the objects created from the CFC

  • Using an object constructor

  • Protecting the inner characteristics of objects with getters and setters

  • Connecting to a database through the internal methods of our object/CFCs

  • A variable structure called a "query", which is used to hold query recordsets

  • Controlling different types...

Our first CFC


We start by creating a file that ends with .cfc when creating CFCs. Non-CFC files that you have been creating up to this point, and will also continue to create, end with .cfm.

Each CFC is wrapped by a set of tags and has one or more functions, which are also referred to as methods. We will be creating an object class. There are different types of objects, and a CFC is the type we normally use in ColdFusion. Now, let us look at some empty code, so you can get an idea of what one might look like:

<cfcomponent>
  <cffunction>
  </cffunction>
</cfcomponent>>

Certainly, we cannot do much with this segment of code. This is just for us to get a first glimpse of CFC code. Here, we get an idea of what it is and start our journey of discovery. You see there is a <cfcomponent> tag that surrounds the whole CFC. You can add additional attributes, but for now we are going to keep it as simple as possible and try to learn how to write CFCs without getting bogged...

Our first object


Our first object is a generic model of a product. There are things that different products have in common. This object will be a combination of the common attributes and methods. The difference is that this is a computer object. Yet, we will do things in ways that reflect the real world. Our product object has attributes and methods. We will map them out here below. Remember, this will all make more sense as we work through this chapter. Along the way, you will get an understanding of both how CFCs work and how to interact with a database.

Product (object)

The attributes are as follows:

  • name

  • description

  • price

The methods are as follows:

  • set_name()

  • get_name()

  • set_description()

  • get_description()

  • set_price()

  • get_price()

We will continue to learn as we work through these examples and we will also continue to refine this to make it even better. We need to build our foundations of understanding first. Now, we will look at an example of this in the following code:

...

Using an object constructor


We will modify our code just a little bit to correct the missing variable using the constructor init() method. Modify the createObject line by adding .init(); to the end as shown in the following highlighted line. You will find that you can tack on a method you want to call at the time of creating an object, all on the same line. This is a common method among developers. When you use a constructor, you should always return the this variable to the caller. this is the variable scope of an object that refers to itself and this returns a reference to the object itself. That will correctly pass the object back when you place the constructor at the end of the creation of the object. If you look at the init method in the CFC, you will find that we include it with the <cfreturn this>.

<!--- Example: 2_2b.cfm --->
<!--- Processing --->
<cfscript>
  objProduct = createObject("component","product_1").init();

You will also notice that we are pulling...

Connecting to a database


Developers used to code the data and presentation into the same file. There was no "data layer" for developing software; it was all mixed on the same page. This made the pages longer and there was much more to work through to figure out bugs or page enhancements. It was pretty much what we call "information overload". CFCs change that by encapsulating the data as a separate layer. Then the interface serves as a simpler way to push and pull data to and from your database.

Note

If you haven't set up your database, then refer to Appendix A and set that part of your environment up for this section first. If you are not skilled with databases, then we have suggestions in Appendix B. You don't need that as we write the ones that you need in the code examples of this book.

Here we will create another version of our product.cfc object class. Let's name it product_2.cfc so we can keep things separate in case you want to go back and compare them later. Let's take a look at the...

Returning data from the CFC


Lastly we return the rsReturn variable to the caller. You will note that this has changed the variable type to a ColdFusion query which is the name for the returned recordset from <cfquery>.

Now we need to create a calling page. We have to see what is coming back from the database and how it looks inside ColdFusion. Here is the code to our calling page.

<!--- Example: 2_4.cfm --->
<!--- Processing --->
<cfscript>
  objProduct = createObject("component","product_2").init();
  rsProducts = objProduct.getRecordset();
</cfscript>
<!--- Content --->
<cfdump var="#rsProducts#">

We see something new in the recordset dump since ColdFusion version 8. This version includes the attributes for cached, execution time, and the SQL that was run to produce the query. These can be very helpful for development, debugging, and logging. You will also see that the result set displays each row in a table. Each of the fields returned from the database...

Making our data query flexible


We set the default value so the page doesn't fail if there is no value for the ID variable passed in. We added the singular getProduct variable. It actually returns a recordset just like rsProducts returns a recordset. The only difference is that this will return a single record after we modify the CFC. Currently the CFC doesn't handle the where argument even though it is part of the method. We have to build in the functionality. This is what the method should look like now. Change the highlighted lines to make this method work correctly. As we are not adding the query type to the <cfoutput> tag inside the if condition where there is only one record, it will not loop through the query. It will only return the first record in the recordset. In this instance there is only one record, so that would be the same. This information is just for future reference, so we will understand why only one row appears on our web page and why we have to declare the query...

The basic data object concept


Now we create the function for assigning the encapsulated variables of the object when it is created. We are now ready to write the next version of the calling page. We are only going to change two things on that page. We will change the line where we were creating a second recordset to a call to the method for performing the assigning of the internal variable values in the object. Then we will change our content section of the code to use the object getters to pull the encapsulated variable values. The highlighted rows are the ones changed.

<!--- Example: 2_6.cfm --->
<!--- Processing --->
<cfparam name="url.id" default="0">
<cfscript>
  objProduct = createObject("component","product_2").init();
  rsProducts = objProduct.getRecordset();
  objProduct.load(url.id);
</cfscript>
<!--- Content --->
<ul><cfoutput query="rsProducts">
  <li><a href="?id=#rsProducts.id#">#rsProducts.name#</a></li&gt...

Object method access control


Generally, objects are not intended to create output other than returning actual variables. That is why the attributes output="false" have been added all over the CFC code. You will also notice that there is an attribute called access="public" in many of the methods. There are actually a number of settings for this. All of these indicate where the calling code must be for the method to run. Here is a list of the settings and definitions for each one:

  • Public: This is the most common and default setting, if not declared. It means that any code on the server can call the object method and it will run.

  • Private: In this case, the method may only be called from within the CFC. In practice, we would have taken our setAttribute() method and made it private. This method would normally be called only from within the actual CFC and never from outside the CFC.

  • Package: This is the condition where code only in the same directory may call the method. The term package comes from...

Summary


One of the biggest advantages of CFCs is the ability to encapsulate logic. This means that we can use the same code over and over without writing it over and over. This makes initial development better. It also makes debugging and updates much more manageable. In this chapter you have just started to get a taste of the power of objects. We will be using them often throughout the rest of this book. This chapter was meant to give you a foundation to understand what follows in the remaining chapters.

Lessons learned in Chapter 2 are:

  • We learned how to create object classes and instantiate object instances.

  • We learned how to create methods, and use the methods and method arguments to interact with objects.

  • We learned how to use object constructors for setting object information at the time of creation.

  • We learned how to use getters/setters to securely control the information inside an object. We also learned that with these methods, you can do more than just setting and getting the values...

Left arrow icon Right arrow icon

Key benefits

  • Fast-paced guide to the foundational concepts of developing in ColdFusion
  • Broad coverage of CFScript to deal with its expanded power in ColdFusion 9
  • Enhance the user interface with built-in ColdFusion AJAX features (layout, forms, script, maps, and more)
  • Packed with example code and real-world knowledge

Description

Adobe ColdFusion is an application server, renowned for rapid development of dynamic websites, with a straightforward language (CFML), powerful methods for packaging and reusing your code, and AJAX support that will get developers deep into powerful web applications quickly. However, developing rich and robust web applications can be a real challenge as it involves multiple processes.With this practical guide, you will learn how to build professional ColdFusion applications. Packed with example code, and written in a friendly, easy-to-read style, this book is just what you need if you are serious about ColdFusion.This book will give you clear, concise, and practical guidance to take you from the basics of ColdFusion 9 to the skills that will make you a ColdFusion developer to be reckoned with. It also covers the new features of ColdFusion 9 like ORM Database Interaction and CF Builder.ColdFusion expert John Farrar will teach you the basics of ColdFusion programming, application architecture, and object reuse, before showing you a range of topics including AJAX library integration, RESTful Web Services, PDF creation and manipulation, and dynamically generated presentation files that will make you the toast of your ColdFusion developer town.This book digs deep with the basics, with real-world examples of the how and whys, to get more done faster with ColdFusion 9.

Who is this book for?

This book is for web developers working with ColdFusion 9. If your goal is to get a good grounding in the basics of the language as quickly as possible and put a site together quickly, this book is ideal for you. This book will also help you if you want to learn more about professional programming of ColdFusion.No prior knowledge of ColdFusion is expected, but basic knowledge of general web and software development skills is assumed.

What you will learn

  • Foundational Concepts of Developing in CFML (ColdFusion Markup Language)
  • How to make your ColdFusion Components (CFCs) come alive with methods, using object inheritance, connecting to a database through the internal methods of your object/CFCs
  • Managing multiple products through common forms for listing, editing, and adding data
  • Debugging and unit testing techniques
  • How to pass information into a custom tag and learn the different methods of storing and accessing tags and tag libraries
  • Enhancing the user interface with built-in ColdFusion AJAX features (layout, forms, script, maps, and so on)
  • Managing files, emails, and images through CFML
  • Power mixing CFCs and custom tags using COOP library as an example to simplify view coding and understand event based objects
  • ORM database interaction covering the new data integration of Hibernate
  • The working of different variable scopes
  • Applications and authenticating users for roles and permissions
  • Integrating Feed, REST, and SOAP web services to interact with other sites

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 21, 2010
Length: 388 pages
Edition : 1st
Language : English
ISBN-13 : 9781849690249
Vendor :
Adobe
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jul 21, 2010
Length: 388 pages
Edition : 1st
Language : English
ISBN-13 : 9781849690249
Vendor :
Adobe
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 142.97
Responsive Web Design with HTML5 and CSS3
$43.99
ColdFusion 9 Developer Tutorial
$54.99
Object-Oriented Programming in ColdFusion
$43.99
Total $ 142.97 Stars icon

Table of Contents

14 Chapters
Web Pages—Static to Dynamic Chevron down icon Chevron up icon
Basic CFCs and Database Interaction Chevron down icon Chevron up icon
Power CFCs and Web Forms Chevron down icon Chevron up icon
ORM Database Interaction Chevron down icon Chevron up icon
Application, Session, and Request Scope Chevron down icon Chevron up icon
Authentication and Permissions Chevron down icon Chevron up icon
CFScript Chevron down icon Chevron up icon
CF AJAX User Interface Chevron down icon Chevron up icon
CF AJAX Forms Chevron down icon Chevron up icon
CF AJAX Programming Chevron down icon Chevron up icon
Introduction to Custom Tags Chevron down icon Chevron up icon
ColdFusion Powered Views Chevron down icon Chevron up icon
Control Logic Processing Chevron down icon Chevron up icon
Guide to Unit Testing Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.8
(4 Ratings)
5 star 25%
4 star 50%
3 star 0%
2 star 25%
1 star 0%
no ego Feb 06, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
What a great ColdFusion Book even in 2020. Just look at CF version adds and depreciations to lead you from this book to current. Hope the authors write a 2020 updated version!!
Amazon Verified review Amazon
Art Luverr Oct 18, 2010
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
The educational method used by the author is different and very to the point. Do as he says, the lessons need to be performed as they are being read to receive the value of the lesson. Written with minimalist background commentary. Refreshing and somewhat foreign, but an effective way to learn higher level coldfusion (CF9). I also liked the brevity of the first book (CF8). A little of this and a little of that covers most bases without ranging too broadly or too deep.Considering the paucity of any Coldfusion books available, it is recommended - another addition to the meager coldfusion library that I have been able to assemble.When is O'Reilly going to update their coldfusion tutorial/appendix contributions? Hunh? I liked their Coldfusion MX book.
Amazon Verified review Amazon
L. Holman May 25, 2013
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
After reading and using Coldfusion since 2006, it was good to have someone other than Ben Forta or Ray Camden write a book about the topic.
Amazon Verified review Amazon
Shelby Hill Jan 13, 2011
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
There were/are tons of typo's in the code samples and throughout. In addition 0 - ZERO effort was made to update the book from Coldfusion 8. The samples and images still have references made to CF 8. Some of the code simply wouldn't work as it was written even after removing the typos. The last 3 chapters of the book were devoted to packages outside of Coldfusion. This bothered me a little as it wasn't a whose who but more of a use this. A tutorial is suppose to teach you how to get up and running and be successful not pimp other group's projects.In the end I had to give it a couple stars because I did manage to learn the material; however, in some cases it's because I had to fix so many typos and figure out the right way to do something. I spent a lot of time looking up issues with Google.And don't even bother looking at the blog they point you to in the beginning of the book. It's simply a shell with no substance. To get the code samples you actually need to go to the Packt publishing website. The samples don't match up very well with the book either. Many times when you look at what he tells you to type and use and then go look at the sample it will have twice as much code in it.In one particular section he doesn't even tell you about a bunch of components you need that he just glosses over. It would have been better to spend a chapter on these extended components and how to do that instead of those other third party tag packages at the end of the book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.