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
Free Learning
Arrow right icon
ColdFusion 9 Developer Tutorial
ColdFusion 9 Developer Tutorial

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

eBook
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781849690256
Vendor :
Adobe
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 107.97
Object-Oriented Programming in ColdFusion
€32.99
ColdFusion 9 Developer Tutorial
€41.99
Responsive Web Design with HTML5 and CSS3
€32.99
Total 107.97 Stars icon
Banner background image

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.