Go Back   SitePoint Forums > Forum Index > Program Your Site > ColdFusion
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Mar 16, 2004, 06:59   #1
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
How to use Coldfusion Components

Okay...

I'm trying to improve my coding skills and practices so I'm looking into CFCs. It would be nice to split out my logic and my display.

I read over Ben Forta's article on how to use CFCs but it seems to be leaving some things out. For example, I've got a CFC that I built. All it does is to pull out the contents of a "page" in my database and display it.

I thought that this would be a simple thing to do to get started. It works when I hard code the page id into the CFC, but I want to be able to pass the page id from the page calling the component. Here's my CFC:
PHP Code:

<cfcomponent hint="">

    <
cffunction name="selectpage" hint="Selects page content from the database based on query string variable.">
        <
cfquery name="getPageInformation" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
            
SELECT *
            
FROM tblpages
            WHERE page_id
= '4';
        </
cfquery>
        <
cfoutput>#getPageInformation.page_content#</cfoutput>
    
</cffunction>
</
cfcomponent>
Here's how I call it:
PHP Code:

<cfinvoke component="getpages" method="selectpage" returnvariable="page" pageid="4"> 

You'll notice that I pass "pageid" into the component. But when I'm in the component, how do I access that argument? I tried this:
WHERE page_id = '#pageid#';

But it doesn't work. What am I doing wrong? Does someone have a good resource for this sort of thing? I really want to learn but nothing I've found on the web so far is really helpful. They all sort of assume that you know what CFCs are already.
__________________
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
creole is offline   Reply With Quote
Old Mar 16, 2004, 07:05   #2
nagrom
SitePoint Guru
 
nagrom's Avatar
 
Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
try:

WHERE page_id = '#arguments.pageid#'

you need them quotes? putting integers in a varchar fields, are we? tsk tsk
__________________
http://www.morgankelsey.com
nagrom is offline   Reply With Quote
Old Mar 16, 2004, 07:11   #3
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
No integers in varchar fields. I know better. I've found though that when I single quote the field, I don't get an error if for some ungodly reason the variable isn't there.

Actually, I found out what I needed, although your way might work as well. In the function, I defined an argument like so:
<cfargument name="pageid" type="numeric" required="true">

Works just fine. I'm going to try your way though. Seems silly to have to define an argument when I'm already passing on in. But, whatever works.

Tested your way and it works as well. I'll use that one instead. Thanks for your help.
__________________
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
creole is offline   Reply With Quote
Old Mar 16, 2004, 09:11   #4
nagrom
SitePoint Guru
 
nagrom's Avatar
 
Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
no problemo...

are you sure you're saving anything? if the query has to be run for every page, why not just put it Application.cfm ?
__________________
http://www.morgankelsey.com
nagrom is offline   Reply With Quote
Old Mar 16, 2004, 12:25   #5
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
Quote:
Originally Posted by creole
PHP Code:

<cfcomponent hint="">

    <
cffunction name="selectpage" hint="Selects page content from the database based on query string variable.">
        <
cfquery name="getPageInformation" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
            
SELECT *
            
FROM tblpages
            WHERE page_id
= '4';
        </
cfquery>
        <
cfoutput>#getPageInformation.page_content#</cfoutput>
    
</cffunction>
</
cfcomponent>
Here's how I call it:
PHP Code:

<cfinvoke component="getpages" method="selectpage" returnvariable="page" pageid="4"> 

if i may, theres a few things you can do to make this a little easier, especially mentally. also it will fix your need to put things in quotes if there is no value.

try something more like this:
Code:
<cfcomponent hint="">
	<cffunction name="selectpage" hint="Selects page content from the database based on query string variable." output="true" returntype="query">
        <cfargument name="pageid" type="numeric" required="true">
		<cfquery name="getPageInformation" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
			SELECT *
			FROM tblpages
			WHERE page_id = #arguments.pageid#;
		</cfquery>
		<!---<cfoutput>#getPageInformation.page_content#</cfoutput>--->
         <cfreturn getpageinformation>
	</cffunction>
</cfcomponent>
and then call it like this...
Code:
  <cfinvoke 
	 component="(put the path to the component here, ask if you need help with this)"
	 method="selectpage"
	 returnvariable="selectpageret">
		<cfinvokeargument name="pageid" value="4"/>
	</cfinvoke>
by doing this you can do several things. first of all by adding the cfargument tag in the component you are requiring that the pageid is given and also that it is numeric. also, you are returning the query as output, so now you can use <cfoutput query="selectpageret"> on the actual page. it makes your code a lot more reusable, and now you can use it in different pages for different functions.

there are several other things you can do and reasons to do it with components, let me know and ill give some more examples. and let me know if you have any trouble with what ive put here
__________________
rynoguill
Ryan Guill, AKA Mark Roman
Rynoguill is offline   Reply With Quote
Old Mar 16, 2004, 17:22   #6
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 32,859
has anybody done timings on components versus inline cfml?

all i can say is, for simplicity, i prefer this method

i save the query as getPageInformation.txt:
Code:
<cfquery name="getPageInformation" ... > 
   select foo, bar, quatsch 
     from tblpages 
    where page_id = #pageid# 
</cfquery>
and i invoke it like this:
Code:
<cfset pageid=4>
<cfinclude template="getPageInformation.txt">
and of course i can use <cfoutput query="getPageInformation">

i can use and reuse this cfquery anywhere, just as easily as that

what am i missing?
__________________
r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
"giving out my real stuffs"
r937 is online now   Reply With Quote
Old Mar 16, 2004, 17:30   #7
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
Quote:
Originally Posted by r937
i can use and reuse this cfquery anywhere, just as easily as that

what am i missing?
the main benifits to me with compents are:
reusability of code,
i only have to change things in my code in one place,
i can use them as webservices,
im an oop kinda guy,
i can do a lot of error checking,
it is really easy to use them on multiple sites, just a few changes to port them,
plus, its the wave of the future man!

and i havent noticed any performance differences, in fact if anything i feel like its faster or at least just as fast as inline code. and i have switch a couple older sites over to components.

just my two cents anyway...
__________________
rynoguill
Ryan Guill, AKA Mark Roman
Rynoguill is offline   Reply With Quote
Old Mar 24, 2004, 05:29   #8
nagrom
SitePoint Guru
 
nagrom's Avatar
 
Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
whelp,

r937 gets down with OPP?
insert into table e!?!?
yeah, you know me!
(bad joke, about bad music)

the real advantage to OOP is you can write functions that EXTEND other functions. uhhhh, inheritance is the word (i think).

but do typical websites require this level of abstraction? prolly not.

if you were working for a hospital, insurance company, or some other giant outfit with a complicated intranet and website you would certainly find situations.

but for simple stuff, i think it's overkill.
__________________
http://www.morgankelsey.com
nagrom is offline   Reply With Quote
Old Mar 24, 2004, 05:47   #9
davidjmedlock
SitePoint Wizard
 
davidjmedlock's Avatar
 
Join Date: Dec 2002
Location: Nashville, TN USA
Posts: 1,775
To be quite honest, I wish I could rewrite my companies entire application with CFMX using components. It's a large app that gets millions of hits a day and it desperately needs restructuring.

For my own websites, though, the only time I may use them is experimentally at first to see what they're like to use.
__________________
WishList.com - Universal Gift Registry

KodeFusion.com | AgentOvation | Web Dev Sucess Blog | Net Realty
davidjmedlock is offline   Reply With Quote
Old Mar 24, 2004, 06:08   #10
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
Rudy...

While I'm not a great judge of speed, it seems to me as if the page loads faster after converting that inline code to a component. Sinec I'm using it in multiple places, it might be caching the bulk of the query.
__________________
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
creole is offline   Reply With Quote
Old Mar 24, 2004, 06:15   #11
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
While I'm on components, here's another question.

I've got banner management built into this new site I'm working on:
http://997wtn.com.icglink.com

In the header I have two banner areas defined, top right (top right corner) and bottom left (under the nav buttons on the left).

I created another component to manage my banner queries; one function to get a random banner, and another function to update that banner's impression count. I would love to use this component in my header, just for consistencies sake but I've got an issue. I have two plain old inline queries right now, one query to get the top banner, and one to get the bottom. Then I have ONE update query which loops through those two banner ids and increments their impression.

How would I go about using components here? I've read that you can invoke the component using the cfobject tag, then CALL it using the cfinoke tag with a name attribute. But it doesn't seem to work.

So, rynogull or anyone else, can you guide me here? How can I use two calls to the same component, returning the same information, just with a different banner_id?


thanks
__________________
Adobe Certified Coldfusion MX 7 Developer
Adobe Certified Advanced Coldfusion MX Developer
My Blog (new) | My Family | My Freelance | My Recipes
creole is offline   Reply With Quote
Old Mar 24, 2004, 06:31   #12
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
straight from the coldfusion 6.1 documentation,
Quote:
When to use CFCs
You can use CFCs in several ways:

To develop structured, reusable ColdFusion code
To create web services
To create elements that can be called by Flash clients
To create services that are accessed directly using a URL or form field
Developing structured, reusable code
Like other CFML code reuse techniques, such as user-defined functions (UDFs) and custom tags, components let you create application functionality that can be reused wherever you need it. If you want to modify, add, or remove component functionality, you make changes in only one component file.

Like UDFs and custom tags, CFCs provide an excellent method for developing structured applications that separate display elements from logical elements and encapsulate database queries.

CFCs have several advantages over UDFs and custom tags. The advantages include all the following "extras" that CFCs automatically provide. (Later sections describe these features in detail.)

The ability to group related functions into a single component, and to group related components into a package
Properties that can be shared by multiple functions
The This scope, a component-specific scope
Inheritance of component methods and properties from a base component, including the use of the Super keyword
Access control
Introspection into CFC functions, properties, and metadata
CFCs have one characteristic that prevents them from being the automatic choice for all code reuse: it takes substantially more processing time to instantiate a CFC than to process a custom tag (and it takes substantially more time to process a custom tag than to execute a user-defined function (UDF)). However, after a CFC is instantiated, calling a CFC method has about the same processing overhead as an equivalent custom tag. As a result, you should not use CFCs in place of independent, single-purpose, custom tags or UDFs. Instead, you should use CFCs to create bodies of related methods, particularly methods that share properties.

For more information about UDFs, custom tags, and other ColdFusion code reuse techniques, see Reusing Code in ColdFusion Pages.
so there you go, it does take more time to instaniate a cfc than a custom tag, but once it is instantiated its about the same. youre only going to notice this though on huge applications and cfc's, i have yet to notice it yet. to me, the ability to confine my code and only have to look one place to change it throughout my entire site. plus with the ability to do oop... i love it
__________________
rynoguill
Ryan Guill, AKA Mark Roman
Rynoguill is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 09:23.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved