Inserts and Updates

Peter Bell recently had a good post about handing inserts and updates . It made me realize that I should discuss the options available for DataMgr to handle these scenarios.

Peter points out that often it is nice to be able to save a record and have the code determine whether to add a new record or update and existing record - this is handled in DataMgr by the saveRecord() method.

He goes on to point out that sometimes you want to be able to explicitely state an insert or an update - DataMgr has insertRecord() and updateRecord() for that respectively.

The motivation here, however, is to go into more detail about your options when saving records with DataMgr.

The updateRecord() method always updates an existing record. It will throw an error if no record exists to update.

The insertRecord() method offers some decisions on how to handle the existence of a matching record via the onexists argument. The possible values for this argument are as follows:

  • insert (DataMgr will try to insert another record despite the existence of a matching record)
  • update (DataMgr will update the existing record)
  • error (DataMgr will throw an error if a matching record already exists)
  • skip (DataMgr will perform no action if a matching record already exists)

It may be interesting to note that saveRecord() internally just calls insertRecord() with onexists=update.

Without adding a ton of methods, this gives you a great deal of flexibility on how to handle existing records when inserting a record.

I will add, however, that I don't worry about these distinctions as much as Peter does. I make sure that my add forms don't pass in a valid primary key, and that ensure that they will always add a new record when saveRecord() is called. 

For more details on how to insert and update records, watch either the "Converting from SQL for Inserts and Updates" or "Converting from CFINSERT and CFUPDate" presentation from my presentations library

DataMgr is free and open-source. You can also take a look at DataMgr 2.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.