We have a request to update certain customers in the ProjTable in the Project Management and Accounting module. And here is the code I have for that
while select NewClient // table that has the customers that need to be udpated
projTable = ProjTable::find(NewClient.ProjId,
projTable.ProjInvoiceProjId = NewClient.PhoenixCustAccount;
projTable.CustAccount = NewClient.PhoenixCustAccount;
projTable.doUpdate(); //I have question here whether to use doUpdate() or update() method of the ProjTable and why?
I am not sure if there is a way to look at the kernel code for update() and doUpdate() to understand to see what these what these methods are doing and decide when to use which method. The statement ‘when we do not want to run the code in update() then use doUpdate() or to bypass the update() method use doUpdate() method’ is not helping much to understand in what scenarios to use update() or doUpdate(). In the above code I also need to call the projTable.initFromCustable( pass custTable… ) to update LocationAddress and Delivery name the projTable record. Do I have to use projTable.Update() here or just the doUpdate() method should do? or per say, if I need to update all most every field in ProjTable then use update() instaed of doUpdate()? Not knowing why and when to use causing some confusion.
If some one could explain in what scenarios doUpdate() or update() make sense with an example should be a big help!
Thanks in advance!
You normally shouldn’t use doUpdate(), exactly because it doesn’t call update().
update() often contain code critical for maintaining database consistency, therefore skipping it often means introducing inconsistency. Surely not a good thing.
doUpdate() is sometimes used for performance reasons, if you know that there is no logic in update() relevant to your change, so you don’t have to execute everything else there. But it’s usually not a good idea anyway - you don’t know what logic will be added later (that would assume that everybody calls update() correctly), e.g. logging all changes for synchronization with another system.
One good reason for calling doUpdate() is when you switch a persistent table to a temporary one. Code in update() usually don’t expect such a situation and calling update() on your temporary table could update related persistent tables.
A side note to the code above: I would join ProjTable and CustTable in the while select, rather then calling two find() methods in each loop.
Thanks for taking time to explain the differences Martin. So, I just use .update() method always even when I just need to update one field in a physical table. And, only use doUpdate() on a temporary tables? It does make sense the existence of doUpdate() and update() methods on the table.
I had a similar situation where I had to create TmpProjInvoiceJour table to temporarily store some data, so created few similar fields that the physical ProjInvoiceJour table has and used doInsert() method for the same reason. However, in our customizations done by our partner developers, I do see lot of doUpdate() methods used on the physical table buffers and that confuses me which one I should use when I ever had a need to update. And I couldn’t figure that out because I do not know if I can see the code for update() and doUpdate() methods.
But your explanation has helped me to understand more concretely that we always need to use only update() method when we are updating the table physical table buffer/variables. And use doUpdate() only when we are dealing with TmpTables. Did I get that right? Also, thanks for the side note.
You can doUpdate() any time when you want to update the record without executing code in update(). This statement isn’t related to the type of table (persistent / temporary).
But you shouldn’t normally skip update() - as I said, one valid scenario is when you’re using a persistent table as temporary and you skip update() because it would otherwise update other persistent tables.