Orm 循环结构和重用EntityNew

Orm 循环结构和重用EntityNew,orm,coldfusion,Orm,Coldfusion,我最近刚开始使用ORM,所以请做好准备,这将是一个简单的答案 因此,我将所有错误检查和参数定义等剥离出来。这是向数据库添加用户的代码部分。实体正在正确填充,所有操作都正常,除了!我得到了完全相同的记录插入了数千次。如果我用一个writeOutput替换实体ORM代码,我得到了预期的结果,但是随着ORM的添加,它实际上变成了一个无休止的循环,并且永远不会更新实体值。我想这是我概念上的缺陷。 想法 companyObj = EntityLoadByPK( 'companyInfo', com

我最近刚开始使用ORM,所以请做好准备,这将是一个简单的答案

因此,我将所有错误检查和参数定义等剥离出来。这是向数据库添加用户的代码部分。实体正在正确填充,所有操作都正常,除了!我得到了完全相同的记录插入了数千次。如果我用一个writeOutput替换实体ORM代码,我得到了预期的结果,但是随着ORM的添加,它实际上变成了一个无休止的循环,并且永远不会更新实体值。我想这是我概念上的缺陷。
想法

    companyObj = EntityLoadByPK( 'companyInfo', companyId );
    for(i=1; i LTE application._.size(toCommit); i = i++ ){
        /**Again, error checking and defining parameters were pulled out for clarity.  
        It's all about the ORM.[I've got to assume]*/

        structClear(currentEntity);
        StructClear( toSaveObjects );

        currentUserName = structFind(toCommit[i],'USERNAME');
        currentEntity = EntityNew( "CompanyUsers");
        currentEntity.setCompanyId(companyObj);

        try{
            properties = getMetaData(currentEntity).properties;
            for(t=1; t <= arrayLen(properties); t++){
                property        = properties[t];
                currentField    = #property.Name#;
                if( StructkeyExists(toCommit[i],property.name)){
                    currentSetValue = structFind(toCommit[i],property.name);
                    if( LEN( TRIM( currentSetValue ) ) ){
                        if( ListFindNoCase( listOfKeys, TRIM( currentField ), "," ) ){
                            if( ListFindNoCase( toCommitOwnFunction, TRIM( currentField ), "," ) ){
                                <! ---[] Requires separate fuction --->
                                if(currentField eq 'userGroups'){
                                    groupObj = EntityLoadByPK( 'companyGroups', 1);
                                    entity_groupObject = EntityNew('companyUsers_J_companyGroups');
                                    entity_groupObject.setUserId(currentEntity);
                                    entity_groupObject.setGroupId(groupObj);
                                }
                            } else{
                                /***BTW, the duplication occurs even if the fields values aren't assigned dynamically.*/
                                evaluate("currentEntity." & "set#property.name#(currentSetValue)");
                            };
                        };
                    };
                };
            };

            transaction {
                EntitySave(currentEntity);
                if(IsDefined('groupObj')){
                    EntitySave(groupObj);
                };
                transactionCommit();
            };
            committedData.message[i]    = TRIM( mssgQualifier ) & TRIM( mssgValue );
        }
        catch(any e){
            committedData.message[i]    = TRIM( mssgQualifier ) & ' failed.  No further information is available.';
        }
    }
    return committedData;
};

我继续在组处理中遇到错误。

使用此解决方案的主要问题是,CF解析器的要求(re:#、evaluate等)似乎违反直觉。首先,我不太喜欢“评估”。无论如何,以下内容解决了这个问题。杰出的是在单独的帖子中维护要跟进的关系实体

        private function commitStruct( required any toCommit, required string toCommitOwnFunction, string shrtName, any companyId ){

        // ORM object to save.  Create for clearing at loop head
        var currentEntity= structNew();

        // committed data struct
        var committed   = structNew();

        // holds the model object properties
        var properties  = structNew();

        // Create default entity object and metadata
        currentEntity   = EntityNew( "CompanyUsers" );
        properties      = getMetaData( currentEntity ).properties;

        //toCommit Counter
        i = 0;

    //------------[START] Primary 'row' loop -------------
        for( row IN toCommit ){

            i++;

            //drop into row var
            row = toCommit[i];

            // Here we are Evicting the entity for reuse.
            ORMEvictEntity("CompanyUsers");

            //create the entity 
            currentEntity   = EntityNew( "CompanyUsers" );

            //getting the cfc column setup and properties
            properties      = getMetaData( currentEntity ).properties;

            try{
                //------------[START] Looping items in row -------------
                for( property IN properties ){
                    //assign the fieldname 
                    current.fieldName   = #property.Name#;
                    //assign the fieldvalue to appropriate var, current.fieldValue
                    current.fieldValue = structFind( row, current.fieldName );

                    // Simple field type insertion, meaning one-to-many, many-to-many, et al would need
                    // to be split off and handled differently.
                    evaluate( "currentEntity." & "set#currentField.fieldName#( currentField.fieldValue )" );

                };
                //.------------[END] Looping items in row -------------

                transaction {

                    // Save the entity
                    EntitySave(currentEntity);

                    transactionCommit();
                };
            }
            catch(any e){
                writeDump(e);
            }
        }
    //.------------[END] Primary 'row' loop -------------
    };

非常感谢@James A Mohler的所有建议。

1。我会使用in循环来让它更清晰。2.currentEntity始终是CompanyUsers实体。我认为没有必要反复获取元数据()。我会把它存储在一个变量中。3.您所做的只是提交groupObj。我会专注于获取好的数据。我可能会在
EntitySave
4之前
writedump
。我看到5个如果嵌套,我想出了一个更简单的方法,或者做了一个函数来处理this@JamesAMohler - 1. 我同意,谢谢。2.currentEntity最终将是动态的,尽管每次引用一次肯定有意义。3.currentEntity将在主要问题之后处理,但我同意,谢谢。4.我从几个当地人那里买来的只是为了展示一下。你说得对,一点也不干净。4对4!:)currentEntity实体的writeDump一次又一次地转储相同的第一条记录。我的假设是,试图重用同一个实体存在问题。你知道这是否是问题所在吗?我认为你是如何循环的,但很难弄清楚什么应该存在,什么时候应该存在。@JamesAMohler感谢James。我会尽量把它剥光并贴出来。保留你的原始代码,然后再附加编辑过的版本。这可能会使问题变得冗长,但它会帮助未来的人理解解决问题的过程。
    property name='userId'
            fieldtype='id,many-to-one'
            displayname='userId'
            cfc='companyUsers'
            fkcolumn='userId'
            hint='The user side of the relationship';
property name='groupId'
            fieldtype='id,many-to-one'
            displayname='groupId'
            cfc='companygroups'
            fkcolumn='groupId'
            hint='The group side of the user/group relationship';
        private function commitStruct( required any toCommit, required string toCommitOwnFunction, string shrtName, any companyId ){

        // ORM object to save.  Create for clearing at loop head
        var currentEntity= structNew();

        // committed data struct
        var committed   = structNew();

        // holds the model object properties
        var properties  = structNew();

        // Create default entity object and metadata
        currentEntity   = EntityNew( "CompanyUsers" );
        properties      = getMetaData( currentEntity ).properties;

        //toCommit Counter
        i = 0;

    //------------[START] Primary 'row' loop -------------
        for( row IN toCommit ){

            i++;

            //drop into row var
            row = toCommit[i];

            // Here we are Evicting the entity for reuse.
            ORMEvictEntity("CompanyUsers");

            //create the entity 
            currentEntity   = EntityNew( "CompanyUsers" );

            //getting the cfc column setup and properties
            properties      = getMetaData( currentEntity ).properties;

            try{
                //------------[START] Looping items in row -------------
                for( property IN properties ){
                    //assign the fieldname 
                    current.fieldName   = #property.Name#;
                    //assign the fieldvalue to appropriate var, current.fieldValue
                    current.fieldValue = structFind( row, current.fieldName );

                    // Simple field type insertion, meaning one-to-many, many-to-many, et al would need
                    // to be split off and handled differently.
                    evaluate( "currentEntity." & "set#currentField.fieldName#( currentField.fieldValue )" );

                };
                //.------------[END] Looping items in row -------------

                transaction {

                    // Save the entity
                    EntitySave(currentEntity);

                    transactionCommit();
                };
            }
            catch(any e){
                writeDump(e);
            }
        }
    //.------------[END] Primary 'row' loop -------------
    };