This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
entity.save( )

entity.save( ) 


 

entity.save ( {mode} ) -> Result 
Parameter Type   Description
mode  Longint in dk auto merge: Enables the automatic merge mode
Result  Object in Result of save operation

The entity.save( ) method saves the changes made to the entity in the table related to its dataClass. You must call this method after creating or modifying an entity if you want to save the changes made to it.

The save operation is executed only if at least one entity attribute has been "touched" (see the entity.touched( ) and entity.touchedAttributes( ) methods). Otherwise, the method does nothing (the trigger is not called).

In a multi-user or multi-process application, the entity.save( ) method is executed under an "optimistic lock" mechanism, wherein an internal locking stamp is automatically incremented each time the record is saved. For more information, please refer to the Entity locking page.

By default, if the mode parameter is omitted, the method will return an error (see below) whenever the same entity has been modified by another process or user in the meantime, no matter the modified attribute(s).
Otherwise, you can pass the dk auto merge option in the mode parameter: when the automatic merge mode is enabled, a modification done concurrently by another process/user on the same entity but on a different attribute will not result in an error. The resulting data saved in the entity will be the combination (the "merge") of all non-concurrent modifications (if modifications were applied to the same attribute, the save fails and an error is returned, even with the auto merge mode).

Note: The automatic merge mode is not available for attributes of picture, object, and text type when stored outside of the record. Concurrent changes in these attributes will result in a dk status stamp has changed error.  

Result

The object returned by entity.save( ) contains the following properties:

PropertyTypeDescription
successbooleanTrue if the save action is successful, False otherwise.
Available only if dk auto merge option is used:
autoMergedbooleanTrue if an auto merge was done, False otherwise.
Available only in case of error:
status(*)numberError code, see below
statusText(*)textDescription of the error, see below
Available only in case of pessimistic lock error:
lockKindTexttext"Locked by record"
lockInfoobjectInformation about the lock origin
task_idnumberProcess id
user_nametextSession user name on the machine
user4d_aliastextUser alias defined with SET USER ALIAS, otherwise user name in the 4D database directory 
user4d_idnumber4D user number (Only available in 4D binary databases
host_nametextMachine name
task_nametextProcess name
client_versiontext
Available only in case of serious error (serious error - can be trying to duplicate a primary key, disk full...):
errorscollection of objects
messagetextError message
componentSignaturetextInternal component signature (e.g. "dmbg" stands for the database component)
errCodenumberError code

(*) The following values can be returned in the status and statusText properties of Result object in case of error:

Constant Value Comment
dk status automerge failed 6 (Only if the dk auto merge option is used) The automatic merge option failed when saving the entity.
Associated statusText: "Auto merge failed"
dk status entity does not exist anymore 5 The entity no longer exists in the data. This error can occur in the following cases:
  • the entity has been dropped (the stamp has changed and the memory space is now free)
  • the entity has been dropped and replaced by another one with another primary key (the stamp has changed and a new entity now uses the memory space). When using entity.drop( ), this error can be returned when dk force drop if stamp changed option is used. When using entity.lock( ), this error can be returned when dk reload if stamp changed option is used

Associated statusText: "Entity does not exist anymore"

dk status locked 3 The entity is locked by a pessimistic lock.
Associated statusText: "Already locked"
dk status serious error 4 A serious error is a low-level database error (e.g. duplicated key), a hardware error, etc.
Associated statusText: "Other error"
dk status stamp has changed 2

The internal stamp value of the entity does not match the one of the entity stored in the data (optimistic lock).

  • with entity.save( ): error only if the dk auto merge option is not used
  • with entity.drop( ): error only if the dk force drop if stamp changed option is not used
  • with entity.lock( ): error only if the dk reload if stamp changed option is not used
Associated statusText: "Stamp has changed"

Creating a new entity:

 C_OBJECT($status;$employee)
 $employee:=ds.Employee.new()
 $employee.firstName:="Mary"
 $employee.lastName:="Smith"
 $status:=$employee.save()
 If($status.success)
    ALERT("Employee created")
 End if

Updating an entity without dk auto merge option:

 C_OBJECT($status;$employees;$employee)
 $employees:=ds.Employee.query("lastName=:1";"Smith")
 $employee:=$employees.first()
 $employee.lastName:="Mac Arthur"
 $status:=$employee.save()
 Case of
    :($status.success)
       ALERT("Employee updated")
    :($status.status=dk status stamp has changed)
       ALERT($status.statusText)
 End case

Updating an entity with dk auto merge option:

 C_OBJECT($status;$employees;$employee)
 
 $employees:=ds.Employee.query("lastName=:1";"Smith")
 $employee:=$employees.first()
 $employee.lastName:="Mac Arthur"
 $status:=$employee.save(dk auto merge)
 Case of
    :($status.success)
       ALERT("Employee updated")
    :($status.status=dk status automerge failed)
       ALERT($status.statusText)
 End case



See also 

entity.drop( )
entity.getStamp( )
entity.isNew( )

 
PROPERTIES 

Product: 4D
Theme: ORDA - Entity

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v17

 
ARTICLE USAGE

4D Language Reference ( 4D v19)
4D Language Reference ( 4D v19.1)
4D Language Reference ( 4D v19.4)
4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)