I am working on migrating a bunch of data from an old database into a new one. In the process of migrating a created a UDF for my script that would give me a bunch of data that I need. When I run a loop that calls the UDF multiple times I find that the first iteration runs fine, but then the UDF disappears from the following iterations.
My code is:
<cffunction name="getCats" access="public">
<cfargument name="assignments" type="any" />
<cfquery name="getCats" datasource="pgdold">
SELECT c1.id AS id1, c1.category AS name1, c2.id AS id2, c2.category AS name2, c3.id AS id3, c3.category AS name3, c4.id AS id4, c4.category AS name4, c5.id AS id5, c5.category AS name5
FROM category c1
LEFT JOIN category AS c2 ON c2.parentid = c1.id
LEFT JOIN category AS c3 ON c3.parentid = c2.id
LEFT JOIN category AS c4 ON c4.parentid = c3.id
LEFT JOIN category AS c5 ON c5.parentid = c4.id
</cfquery>
<cfquery name="get" dbtype="query">
SELECT (name1 + '/' + name2 + '/' + name3 + '/' + name4 + '/' + name5) AS category
FROM getCats
WHERE
<cfloop query="Arguments.assignments">
(id1 = #Arguments.assignments.categoryid# OR id2 = #Arguments.assignments.categoryid# OR id3 = #Arguments.assignments.categoryid# OR id4 = #Arguments.assignments.categoryid# OR id5 = #Arguments.assignments.categoryid#)
<cfif Arguments.assignments.currentrow IS NOT Arguments.assignments.recordCount> OR </cfif>
</cfloop>
</cfquery>
<cfreturn get />
</cffunction>
<cfscript>
olddb = {datasource='pgdold'};
a = new Query(argumentCollection=olddb);
a.setSQL('SELECT * FROM products LIMIT 10');
p = a.execute();
pr = p.getResult();
</cfscript>
<cfscript>
products = arrayNew();
for(i=1;i<=pr.recordCount;i++){
product = {};
if(!reFind('([0-9]+\-)(G|g)(I|i)(F|f)(T|t)',pr['sku'][i])){
b = new Query(argumentCollection=olddb);
b.setSql('SELECT * FROM productpics WHERE sku = :sku ORDER BY picorder');
b.addParam(name='sku',value=pr['sku'][i]);
pics = b.execute().getResult();
picList = '';
for(j=1;j<=pics.recordCount;j++){
picList = picList & ';' & pics['imagename'][j];
}
d = new Query(argumentCollection=olddb);
d.setSql('SELECT * FROM skucategories WHERE sku = :sku');
d.addParam(name='sku', value=pr['sku'][i]);
assignments = d.execute().getResult();
categories = getCats(assignments);
writeDump(categories);
product = {
store = 'admin',
websites = 'base',
attribute_set = 'Default',
categories = '',
type = 'simple',
sku = pr['sku'][i],
name = reReplace(reReplace(pr['title'][i],'\"','&##34;','all'),'\,','&##44;','all'),
price = pr['price'][i],
description = reReplace(reReplace(pr['detail'][i],'\"','&##34;','all'),'\,','&##44;','all'),
short_description = '',
image = pics['imagename'][1],
small_image = pics['imagename'][1],
thumbnail = pics['imagename'][1],
weight = pr['weight'][i],
has_options = 1,
is_in_stock = 1,
qty = 1000,
disabled = 'No',
status = 'Enabled',
options_container = 'Black after info Column',
tax_class_id = 'Taxable Goods',
visibility = 'Catalog,Search',
gallery = right(picList,len(picList)-1) // Seperate images by semicolon (;)
};
arrayAppend(products,product);
}
}
</cfscript>
It is the getCats function that disappears.
* yes the code is ugly and inefficient. It wasn’t meant to do anything more than this one job and after it finishes the job it is going to be thrown away, so don’t tell me about the ugliness or inefficiencies.
Without reading every line of your code posted, I have a Strong feeling that it has to do with Var scooping… since you are calling that UDF in a Loop.
Please var scope you query variables.
Pre CF9:
At, or Post CF9 with
Localscope: