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

CFC Question

Participant ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Can you specify which database fields you want returnd to you when calling a CFC?

Let's say I have a employee table with 50 fields in it. I only want the first_name and last_name fields when I call a function GETNAME within my employee.cfc using EmpID as my argument.

Can I specify the first_name and last_name as arguments to be returned to me? if so, how?
TOPICS
Advanced techniques

Views

622

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

Participant , Nov 15, 2007 Nov 15, 2007
Excellent.
Let me try it and see if I can get it working.

Thanks so much.

Votes

Translate

Translate
Advisor ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Have the function in your CFC return a query object or a struct containing first and last name elements.

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
Participant ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Thank Bob,

I was actually thinking of making it generic enough so if the requestor wanted "Last_name" and "Dept", then I would return that and if they wanted "Dept" and "Manager_Name", then I would return that. So the caller of the cfc would be in control and ask for what field they want.

So is there a way to tell the object what you want to have returned back?

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
Advisor ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

You could provide the field names desired as an array in an argument then build the query as shown below. I do not recommend this however as you are vulnerable to SQL injection attack unless you are careful about what gets into your array.

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
Advisor ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Alternatively if you only execpt one row to be returned you could use a struct to return only the fields you desire.

See below



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
Participant ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Thanks Bob,

What you have provided me is great.
Would I then call my cfc using the code below?
How would I know what the developer is going to be asking for? I need a way for them to tell me from the <cfinoke> in order for the CFC to know what to return. The calling part is what is puzzling me. The ability to accept different field name from the caller and send them back what they asked for.



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
Advisor ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

See code below

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
Participant ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Excellent.
Let me try it and see if I can get it working.

Thanks so much.

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 ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

Minor typo correction. Don't forget to VAR the function variables :-)

<cfset VAR local=StructNew() />

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
Advisor ,
Nov 15, 2007 Nov 15, 2007

Copy link to clipboard

Copied

LATEST
Good catch cf_dev2. Yes, always var scope within cffunction.

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