UDF消失了

时间:2011-06-14 17:31:00

标签: coldfusion iteration user-defined-functions

我正在努力将旧数据库中的大量数据迁移到新数据库中。在为我的脚本迁移创建的UDF的过程中,这将为我提供一堆我需要的数据。当我运行一个多次调用UDF的循环时,我发现第一次迭代运行正常,但随后UDF从以下迭代中消失。

我的代码是:

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

getCats函数消失了。

*是的,代码是丑陋而低效的。这不仅仅意味着做这项工作,而且在完成工作后它将被抛弃,所以不要告诉我丑陋或效率低下。

2 个答案:

答案 0 :(得分:2)

如果没有阅读发布的代码的每一行,我都会感觉强烈与Var scooping有关...因为你在循环中调用UDF。

请查询变量范围。

前CF9:

<!--- insert after <cfargument> --->
<cfset var getCats = "">
<cfset var get = "">

At,或发布带有Local范围的CF9:

<cfquery name="local.getCats" datasource="pgdold">
<cfquery name="local.get" dbtype="query">

答案 1 :(得分:2)

您在UDF中为UDF命名了一个与UDF同名的变量。 var范围或将该查询重命名为其他内容。我建议将其命名为其他内容。