我最近才刚开始使用ORM,因此请为此做一个简单的答案...
因此,我删除了所有错误检查和参数定义等。这是将用户添加到数据库的代码的一部分。实体已正确填充,一切正常,除了!我得到的完全相同的记录已插入数千次。如果我用writeOutput替换Entity 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;
};
因此,我尝试清理以上代码。我还遇到了OrmEvictEntity,它解决了我遇到的主要问题。
private function commitStruct( required any toCommit, required string toCommitOwnFunction, string shrtName, any companyId ){
// Struct Key List. May vary by record
var listOfKeys = '';
// ORM object to save. Create for clearing at loop head
var currentEntity = structNew();
// anncillary/related obcts to save with entity
var entityObjsToSave = structNew();
// The temp subject of the return msg
var msgSubject = '';
// The temp action of the return msg
var msgAction = '';
// committed data struct
var committed = structNew();
// msgSubject and msgAction return struct
var committed.returnMsg = structNew();
// holds the model object properties
var properties = structNew();
//current row username, if applicable
var currentUserName = '';
// if entity required companyId relationship
if( ListFindNoCase( this.typeNeedsCompId, shrtName) ){
var companyObj = EntityLoadByPK( 'companyInfo', companyId );
};
// If we are Adding Users (AU), create the entity and get bean metadata
if( ListFindNoCase( 'AU,UU', TRIM( shrtName ), ',' , false ) ) {
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];
// clear primaries--Future Use.
structClear( entityObjsToSave );
currentUserName = '';
msgSubject = '';
msgAction = '';
//Update listOfKeys for this record
listOfKeys = StructKeyList(toCommit[i]);
//assign username for frequent, future reference
if( ListFindNoCase(listOfKeys, 'USERNAME' ) ){
currentUserName = structFind( row, 'USERNAME' );
}
// If we are Adding Users (AU) or Updating Users (UU), create the entity and assign default msg values
if(shrtName eq 'AU'){
ORMEvictEntity("CompanyUsers");
currentEntity = EntityNew( "CompanyUsers" );
msgSubject = TRIM( currentUserName );
msgAction = ' - submitted successfully';
}
properties = getMetaData( currentEntity ).properties;
//------------[START] FUTURE CONDITIONS -------------
//.------------[END] FUTURE CONDITIONS -------------
// Set companyId to entity
if( ListFindNoCase( this.typeNeedsCompId, shrtName ) ){
currentEntity.setCompanyId( companyObj );
};
try{
//------------[START] Looping items in row -------------
for( property IN properties ){
currentField = setFieldNameAndValue( property, row, listOfKeys );
//if this field was not ruled out for some reason previously but caught locally
if( currentField.fieldValue NEQ 'IGNORE' ){
// if the field name is listed in toCommitOwnFunction, split off for separate processing
if( ListFindNoCase( toCommitOwnFunction, TRIM( currentField.fieldName ), "," ) ){
// test toCommitOwnFunction field names
switch (currentField.fieldName){
case 'userGroups':
if( isDefined( 'groupObj' ) ){
ORMEvictEntity('groupObj');
}
if( isDefined( 'entity_groupObject' ) ){
ORMEvictEntity("entity_groupObject");
};
groupObj = EntityLoad( 'companyGroups', { groupName = #currentField.fieldValue# });
entity_groupObject = EntityNew('companyUsers_J_companyGroups');
entity_groupObject.setUserId(currentEntity);
entity_groupObject.setGroupId(groupObj);
break;
default:
msgAction = '-Error. Unexpected Field';
}
}
else{
if( LEN(TRIM(currentField.fieldValue))){
//Simple field type insertion
evaluate( "currentEntity." & "set#currentField.fieldName#( currentField.fieldValue )" );
};
};
};
};
//.------------[END] Looping items in row -------------
transaction {
EntitySave(currentEntity);
if( !isNull( entity_groupObject ) ){
EntitySave( entity_groupObject );
};
transactionCommit();
};
committed.returnMsg[i] = TRIM( msgSubject ) & TRIM( msgAction );
}
catch(any e){
committed.returnMsg[i] = TRIM( msgSubject ) & ' - Action Failed. No additional information is available.<br/><br/>' & e; }
}
//.------------[END] Primary 'row' loop -------------
return committed;
};
我唯一剩下的问题是groupObj,这仅仅是由于我确定缺乏ORM经验。
我有用于CompanyUsers的cfcs,在companyUsers_J_companyGroups和与之相同的CompanyGroups中表达了一对多(返回到companyUsers_J_companyGroups)。 companyUsers_J_companyGroups向CompanyUsers和CompanyGroups指出了多对一的关系-
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';
我继续在群组处理中遇到错误。
答案 0 :(得分:1)
我遇到的这个解决方案的主要问题是CF解析器的要求(例如:#,求值等等)似乎违反直觉。首先,我不喜欢“评估”。无论如何,以下解决了该问题。杰出之处在于保持关系实体在单独的帖子中进行跟进。
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的所有建议。