Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / Advanced Techniques / October 2007



Tip: Looking for answers? Try searching our database.

component calling methods on another component

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dzacharias - 30 Oct 2007 22:58 GMT
I have 2 components: Repository, and Document. A repository has an array of
documents in variables scope. In each component I use the init() method to
return an instance of the object. When a repository object is inited it
generates a query of the documents that it contains, and loops through that,
calling document.init() on each one and adding the resulting document object to
the array.

When I init() a repository and use CFDUMP to view its documents array, I get
an array of the correct length containing document objects. However, when I try
to loop through the array and call a method like getTitle() on each document, I
get empty strings. When I init() a document on its own, getTitle() works fine.
Is there something basic that I'm forgetting here?

Let me know if you need to see the more code.

Don Zacharias
Sacramento, CA
Dan Bracuk - 31 Oct 2007 00:19 GMT
I always use cfobject or createobject to return an instance of the cfc.  Have you considered trying that?
dzacharias - 31 Oct 2007 00:24 GMT
Yeah I am using the pattern of an init() method that returns an instance of
itself, so it can be chained to the createObject call. It seems to append an
object, but I can't figure out how to work with that object. Any ideas? Thanks!!

<!--- in the Repository's init() method --->
<cfloop query="qDocuments">
  <cfset document = createObject("component","Document").init(qDocuments.id)>
  <cfset arrayAppend(variables.documents,document)>
</cfloop>
cf_dev2 - 31 Oct 2007 00:59 GMT
So where are you setting the other document properties like "Title"?
dzacharias - 31 Oct 2007 19:12 GMT
In the Document's init method, those properties are pulled from the database
and set in variables scope. Then I use getTitle() etc to pull it back out.

And yeah those work fine when I'm just initing a document by itself, it's just
when I am trying to init a bunch of documents in the repository's init method,
and then call methods on those documents.

I feel like I'm missing something very basic here... Thanks!
dzacharias - 31 Oct 2007 19:18 GMT
OMG I just nailed it. On the front end template, I tried using createObject
each time through the loop of documents, and then setting the resulting empty
document object to the document in the array. That worked. Thanks everybody!
cf_dev2 - 31 Oct 2007 19:29 GMT
[q][i]Originally posted by: [b][b]dzacharias[/b][/b][/i]
OMG I just nailed it. On the front end template, I tried using createObject
each time through the loop of documents, and then setting the resulting empty
document object to the document in the array. That worked. Thanks everybody![/q]

I'm not sure how that's different than what you were doing before ;-) but I'm
glad its working for you.

<cfloop query="qDocuments">
   <!--- create empty document object --->
  <cfset document = createObject("component","Document").init(qDocuments.id)>
   <!--- add empty document to array --->
  <cfset arrayAppend(variables.documents,document)>
</cfloop>
dzacharias - 31 Oct 2007 19:39 GMT
In the Repository's init() method:
<cfloop query="qDocuments">
<!--- create empty document object --->
<cfset document = createObject("component","Document").init(qDocuments.id)>
<!--- add empty document to array --->
<cfset arrayAppend(variables.documents,document)>
</cfloop>

In the front end
<!--- show documents for this repository --->
<cfset arr_documents = repository.getDocuments()>
<cfloop from="1" to="#arrayLen(arr_documents)#" index="i">
    <cfset doc = createObject("component","/docmanager_v2/components/Document")>
    <cfset doc = arr_documents[ i ]>
    <cfoutput>Title: #doc.getTitle()#, Summary: #doc.getSummary()#, Keywords:
#doc.getKeywords()#<br></cfoutput>
</cfloop>

Now I'm doing createObject twice but I guess that makes sense... I wish I knew
the principle at work here though...
cf_dev2 - 31 Oct 2007 00:52 GMT
dzacharias,

I don't have an answer, but ... are you actually creating a new document
instance within each loop of your query? If you're not, the array would be
populated with "x" number of references to the same object. Since they are
references, what happens to one happens to all of them.  So if the "Title" of
one document object is blank, all of them are blank.
cf_dev2 - 31 Oct 2007 20:26 GMT
You shouldn't have to create the objects twice.  I'm not even sure why that
would work any differently,  since in the end you're using the object from the
array anyway.

<!--- the variable "doc" is overwritten in the next line --->
<cfset doc = createObject("component","/docmanager_v2/components/Document")>
<cfset doc = arr_documents[ i ]>

Also, are you properly VAR'ing your function local variables?
dzacharias - 31 Oct 2007 20:39 GMT
Actually you're right... I don't know why but I must not have tried it that
way. Each time through the array I just copy the array element to a new
variable and access the document methods through that variable and it works
fine. Thanks again!
cf_dev2 - 31 Oct 2007 20:53 GMT
Okay.  Remove the second createObject() then.  No point incurring the expense
of createObject() if its not needed ;-)

> I just copy the array element to a new variable and access the document
methods
> through that variable and it works fine.

IIRC its just a reference to the array element at that index, but yes that
works :)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.