When<cftransaction>begins, any existing ORM session is flushed and closed, and a new ORM session is created.
var item = entityLoadByPK( 'Your_Entity', 'id' );
// update a property
item.setActive( !item.getActive() );
// the orm session is flushed because a new transaction is opened
transaction {
// correct me if I'm wrong but - nothing in here matters?
}Jacob,
You seem to be using native ORM methods, along with the ORM service methods. When using cbORM, you should avoid the native CF ORM methods and use the Base ORM service methods to load and work with entities. ( e.g.
getInstance( “myEntity” ).get( myId ) rather than
entityLoadByPK( 'myEntity’, myId ) ).
Any saves called by the BaseORMService are automatically transactioned, unless you specify otherwise, or they are already within a transaction. This ensures that procedural blocks with
subsequent save() calls can depend on the database record existing and saves a whole bunch of manual flushing of the session.
If you don’t wish to use transactions, you can bypass that:
Active Entity : myEntity.save( transactional=false )
Service : myService.save( entity=myEntity, transactional=false)
An empty transaction block would not be the way to force a flush of the session. You can use the the save() or saveAll() methods and pass a `flush=true` argument or just rely on the transaction to flush.
If you have a bunch of items being saved within a loop, simply wrap all of those items in one transaction and pass the `transactional=false` argument to the `save()` call. Then commit the transaction at the end.
So, to give you an example, using your code ( which will automatically transaction your entity save ):
var itemService = new cborm.models.VirtualEntityService( entityName=“Item” );
var item
= itemService.get(
itemId );
// update and save
item.setActive(
!item.getActive()
).save();
If you want to do other things before you make the big save:
transaction{
item.setActive(
!item.getActive()
).save( transactional=false );
… do lots of things …
transaction action=”commit”;
}
HTH,
Jon
--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit
https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/coldbox/9ca794d0-d89d-4826-b378-110754bc0ed3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
public any function updateItem( required Any event, required Struct rc, required Struct prc ) {
var item0 = itemService.get( '0' );
item0.setActive( !item0.getActive() );
itemService.save( item0 );
return {
done: true
};
} public any function updateItem( required Any event, required Struct rc, required Struct prc ) {
var item0 = itemService.get( '0' );
var item1 = itemService.get( '1' );
item0.setActive( !item0.getActive() );
// this one is also updated, but not passed into the save function
// it WILL be updated in the database as well
item1.setActive( !item1.getActive() );
itemService.save( item0 );
return {
done: true
};
}