• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

component calling methods on another component

Community Beginner ,
Oct 30, 2007 Oct 30, 2007

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

967

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Beginner , Oct 31, 2007 Oct 31, 2007
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!

Votes

Translate

Translate
LEGEND ,
Oct 30, 2007 Oct 30, 2007

Copy link to clipboard

Copied

I always use cfobject or createobject to return an instance of the cfc. Have you considered trying that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 30, 2007 Oct 30, 2007

Copy link to clipboard

Copied

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!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 30, 2007 Oct 30, 2007

Copy link to clipboard

Copied

So where are you setting the other document properties like "Title"?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

quote:

Originally posted by: dzacharias
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!


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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 30, 2007 Oct 30, 2007

Copy link to clipboard

Copied

dzacharias,

Nevermind. I see from your post you are creating new instances.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 31, 2007 Oct 31, 2007

Copy link to clipboard

Copied

LATEST
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 🙂


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation