It's possible I am not doing this right at all.
I am initializing and storing into the session scope an object which contains details of the currently logged in user.
The CFC is called Provider, and when the user succesfully logs in I call:
session.Provider = New model.Provider().init(loginResult.ProviderID);
I have a method in the CFC called resetValues, which is to set the specified values to an empty string, but I can't work out how to save the entity once I have reset the values.
Here is the full CFC. I am attempting to save with entitySave( this );
, but not working. Any suggestions on how I should save (persist) this changes? Or do I need to completely rethink how I am going to change these values?
Many Thanks in advance.
component persistent="true" table="provider" {
property name="id" fieldtype="id" column="provider_id";
property name="title" ormtype="text";
property name="status" ormtype="integer";
property name="email" ormtype="text";
property name="ext_src" ormtype="text";
property name="ext_src_login1" ormtype="text";
property name="ext_src_login2" ormtype="text";
property name="ext_src_login3" ormtype="text";
property name="ext_src_login4" ormtype="text";
//property name="rooms" fieldtype="one-to-many" cfc="Room" fkcolumn="provider_id";
//init()
public function init(providerID="0"){
variables.providerID = arguments.providerID;
return this;
}
//get()
public function get(){
return entityLoadByPK("Provider",variables.providerID);
}
//resetValues()
public function resetValues(){
this.setExt_src_login1('');
this.setExt_src_login2('');
this.setExt_src_login3('');
this.setExt_src_login4('');
entitySave(this);
}
}
Im getting the following error: a different object with the same identifier value was already associated with the session
Couple of things i see wrong with this
its not advisable to store ORM entites in a scope that persists between requests, you will run into problems with detached entities (see Things to watch out for in ColdFusion 9 with CF-ORM). An ORM entity is attached to a hibernate session (not to be confused with a cf session) which does not span requests, once the requests end the entity you've assigned to session will become detached. Store the providerid in the session and pull the entity in each request. If that seems like too much overhead, just store the things you need for a basic request, like id and name, then only load the entity when your actually performing an operation that requires it.
you shouldn't modify a field with type of 'id' (unless your using provider="assigned").
Inside a hibernate session only one instance of an entity with a given identifier can exist, so by setting the id of your entity and then trying to do entityLoadByPK() on the same id you're confusing hibernate.
If you want to have a get() method to retrieve a particular entity by id, its best to place this in a service or factory. Its easy to create an abstract service with generic methods for getting or finding entities which you can then extend with methods for your specific needs. Coldspring 2 includes an AbstractGateway which is a great starting point - http://sourceforge.net/apps/trac/coldspring/wiki/ORMAbstractGateway
The TLDR; fix is to do
session.provider = entityLoadByPK("Provider", loginResult.ProviderID);