[coldbox-3.5.0-BE] Really simple and weird active entity issue

The below code is a simplified version of something I do numerous times in the handler of the application I’m currently working on. What’s odd is that the code below will only save if I remove the conditional. From what I can tell when the conditional is present the save method isn’t executed. When the conditional is removed everything works as expected. The conditional is evaluating true and the var is properly set into the object - it simply doesn’t save when it’s wrapped up like this. I’m baffled!

if(true) {

var project = ProjectService.get(“Project”,projectId);
project.setProjectName(rc.projectName);

project.save();
}

I think this is a ColdFusion problem, there was a thread sometime ago about it failing under certain conditions or something. Not sure if it was in here or the cftalk mailing list.

Also ColdBox 3.5 is no longer BE so you might want to upgrade / update to that. Not that, that will fix this issue though.

Thanks for the quick response Andrew.

I think I figured something out - but this is definitely a weird cf issue. The form that is posting to the handler is being submitted via an ajax post. To get the handler method to work I removed the Id of the project object from the hidden form field and added it to the foms action url. Accessing the Id via ses instead of the one that was getting posted with the forms data made everything work as expected?

What’s really odd is that CF was getting the obj from the database with the Id coming in directly from the hidden form field (I could see the data in the console and dump it out), but for whatever reason it wouldn’t persist the object. I don’t know what the difference is because on the outside both versions of the Id look identical (it’s a guid btw). Who knows!

Thanks again,

Sam

So the if(true) was actually running then?

Yes - with the conditional running and the Id of the object hitting the handler in the URL instead of the post data it works. I don’t know what’s any different because it finds the object in the db regardless of how the Id is sent to the handler but one way saves and the other doesn’t.

When you say ID do you mean projectID? is there any reason you are not referencing this with the full scope like rc.projectId or is this coming from somewhere else?

This is by far the weirdest thing I’ve come across this year! I have to think it has something to do with the Id being a guid and how the ajax post was sending it to the server. It’s just odd that it cf/hibernate finds the record in the db regardless of how the Id is sent in but that it won’t save the object it if the Id is coming directly from the ajax submit.

Problem is Sam you don’t give us much to work with, and you didn’t answer my last question.

Problem is Sam you don’t give us much to work with, and you didn’t answer my last question.

Sorry Andrew. Yes the Id I was referencing is the projectId. Pulling it out of the url instead of the rc with the rest of the form data is what made the difference. Sorry if I was cryptic. Again, the ORM found the record in the db regardless of how the projectid was sent to the handler - but it would only save the project object when I passed the projectId from the url to the handler and then on to the service method. My ProjectService is extending the Coldbox baseORMService which is where the get method is located.

This is the method that’s working:

var projectId = event.getValue(“id”); // this is getting plucked out of the url

if(true){

var project = ProjectService.get(“Project”,projectId);
project.setProjectName(rc.projectname);

project.save();

}

Here’s the version of the method from earlier that wouldn’t save

if(true){

var project = ProjectService.get(“Project”,rc.projectId);
project.setProjectName(rc.projectname);

project.save();

}

Ok that is weird…

I am assuming it will have something to do with the Ajax call, at this stage. Are you doing a post or get with the Ajax?

I digress - I got to work this morning and whatever was allowing it work last night has ceased to work today. The only way I can get this to work with the conditional in the handler method is to wrap it in a transaction. The form data is coming in via a post and everything checks out as far as the rc is concerned (the data is all there and accessible). Using the transaction I removed the project.save() to get it to work.

If I output the chain of execution when I call project.save() inside of the conditional (without the transaction) the method doesn’t appear to be firing. i.e. I placed a console dump inside the ActiveEntity’s save method and nothing ever makes it the console. Again - project.save() does work if the conditional isn’t present in the handler.

I also believe it has something to do with how the data is getting posted to the handler via the ajax post. But - conditional or not, hibernate successfully pulls the record from the database regardless of how the projectId is passed to the handler.

I am using windows 7 (IIS 7.5) with coldfusion 9.0.1 and the development branch of Coldbox. I did revert back to the 3.5 release earlier today with the same results.

I am at a total loss.

Here is the full working method as it stands right now.

function save(event, rc, prc) {

prc.results = validateModel(target=rc, constraints=“admin_SaveProject”);

transaction
{

if(!prc.results.hasErrors()) {

var project = ProjectService.get(“Project”, rc.projectId);
var projectType = ProjectService.get(“ProjectType”, rc.ProjectType);
var internalCo = ProjectService.get(“InternalCompany”, rc.InternalCompany);

project.setIsPrivate(rc.isPrivate);
project.setProjectType(projectType);
project.setInternalCompany(internalCo);
project.setProjectName(event.getTrimValue(“ProjectName”));
project.setProjectNumber(event.getTrimValue(“projectNumber”));
project.setOpportunityNumber(event.getTrimValue(“OpportunityNumber”));
project.setIsRecoveryFunded(rc.IsRecoveryFunded);
project.setProjectDescription(event.getTrimValue(“ProjectDescription”));
project.setSubmittalRequirements(event.getTrimValue(“SubmittalRequirements”));
project.setAdditionalInformation(event.getTrimValue(“AdditionalInformation”));
project.setBidInviteIntro(event.getTrimValue(“BidInviteIntro”));

transaction action=“commit”;

event.renderData(type=“JSON”, data={success=1});
} else {
event.renderData(type=“JSON”, data=prc.results.getAllErrors());
}
}
}

Two things…

One when doing the validation, I would not stick the result in the PRC as there is no need for it to be there, as this is meant to be used only in the handler then I would make it a local var.

Two, just out of curiousity did you try dumping the result.hasResults()?

In cases like this I like to use the ColdFusion Builder debugger, fire the break point at the condition and inspect the variables and step over it, you will be surprised how many times that will show you things you may not see.