Entity
An entity is an instance of a Dataclass, like a record of the table matching the dataclass in its associated datastore. It contains the same attributes as the dataclass as well as the data values and specific properties and functions.
Summary
.attributeName : any stores the attribute value for the entity |
.clone() : 4D.Entity creates in memory a new entity referencing the same record as the original entity |
.diff( entityToCompare : 4D.Entity { ; attributesToCompare : Collection } ) : Collection compares the contents of two entities and returns their differences |
.drop( {mode : Integer} ) : Object deletes the data contained in the entity from the datastore |
.first(): 4D.Entity returns a reference to the entity in first position of the entity selection which the entity belongs to |
.fromObject( filler : Object ) fills an entity with the filler content |
.getDataClass() : 4D.DataClass returns the dataclass of the entity |
.getKey( { mode : Integer } ) : Text .getKey( { mode : Integer } ) : Integer returns the primary key value of the entity |
.getRemoteContextAttributes() : Text returns information about the optimization context used by the entity |
.getSelection(): 4D.EntitySelection returns the entity selection which the entity belongs to |
.getStamp() : Integer returns the current value of the stamp of the entity |
.indexOf( { entitySelection : 4D.EntitySelection } ) : Integer returns the position of the entity in an entity selection |
.isNew() : Boolean returns True if the entity to which it is applied has just been created and has not yet been saved in the datastore |
.last() : 4D.Entity returns a reference to the entity in last position of the entity selection which the entity belongs to |
.lock( { mode : Integer } ) : Object puts a pessimistic lock on the record referenced by the entity |
.next() : 4D.Entity returns a reference to the next entity in the entity selection which the entity belongs to |
.previous() : 4D.Entity returns a reference to the previous entity in the entity selection which the entity belongs to |
.reload() : Object reloads the content of the entity in memory |
.save( { mode : Integer } ) : Object saves the changes made to the entity |
.toObject() : Object .toObject( filterString : Text { ; options : Integer} ) : Object .toObject( filterCol : Collection { ; options : Integer } ) : Object returns an object which has been built from the entity |
.touched() : Boolean tests whether or not an entity attribute has been modified since the entity was loaded into memory or saved |
.touchedAttributes() : Collection returns the names of the attributes that have been modified since the entity was loaded into memory |
.unlock() : Object removes the pessimistic lock on the record matching the entity |
.attributeName
History
Release | Changes |
---|---|
17 | Added |
.attributeName : any
Description
Any dataclass attribute is available as a property of an entity, which stores the attribute value for the entity.
Dataclass attributes can also be reached using the alternate syntax with [ ].
The attribute value type depends on the attribute kind (relation or storage):
- If attributeName kind is storage:
.attributeName
returns a value of the same type as attributeName. - If attributeName kind is relatedEntity:
.attributeName
returns the related entity. Values of the related entity are directly available through cascading properties, for example "myEntity.employer.employees[0].lastname". - If attributeName kind is relatedEntities:
.attributeName
returns a new entity selection of related entities. Duplications are removed (an unordered entity selection is returned).
Example
var $myEntity : cs.EmployeeEntity
$myEntity:=ds.Employee.new() //Create a new entity
$myEntity.name:="Dupont" // assign 'Dupont' to the 'name' attribute
$myEntity.firstname:="John" //assign 'John' to the 'firstname' attribute
$myEntity.save() //save the entity
.clone()
History
Release | Changes |
---|---|
17 | Added |
.clone() : 4D.Entity
Parameter | Type | Description | |
---|---|---|---|
Result | 4D.Entity | <- | New entity referencing the record |
Description
The .clone()
function creates in memory a new entity referencing the same record as the original entity. This function allows you to update entities separately.
Keep in mind that any modifications done to entities will be saved in the referenced record only when the
.save( )
function is executed.
This function can only be used with entities already saved in the database. It cannot be called on a newly created entity (for which .isNew()
returns True).
Example
var $emp; $empCloned : cs.EmployeeEntity
$emp:=ds.Employee.get(672)
$empCloned:=$emp.clone()
$emp.lastName:="Smith" //Updates done on $emp are not done on $empCloned
.diff()
History
Release | Changes |
---|---|
17 | Added |
.diff( entityToCompare : 4D.Entity { ; attributesToCompare : Collection } ) : Collection
Parameter | Type | Description | |
---|---|---|---|
entityToCompare | 4D.Entity | -> | Entity to be compared with the original entity |
attributesToCompare | Collection | -> | Name of attributes to be compared |
Result | Collection | <- | Differences between the entities |
Description
The .diff()
function compares the contents of two entities and returns their differences.
In entityToCompare, pass the entity to be compared to the original entity.
In attributesToCompare, you can designate specific attributes to compare. If provided, the comparison is done only on the specified attributes. If not provided, all differences between the entities are returned.
The differences are returned as a collection of objects whose properties are:
Property name | Type | Description |
---|---|---|
attributeName | String | Name of the attribute |
value | any - Depends on attribute type | Value of the attribute in the entity |
otherValue | any - Depends on attribute type | Value of the attribute in entityToCompare |
Only attributes with different values are included in the collection. If no differences are found, .diff()
returns an empty collection.
The function applies for properties whose kind is storage or relatedEntity. In case a related entity has been updated (meaning the foreign key), the name of the related entity and its primary key name are returned as attributeName properties (value and otherValue are empty for the related entity name).
If one of the compared entities is Null, an error is raised.
Example 1
var $diff1; $diff2 : Collection
employee:=ds.Employee.query("ID=1001").first()
$clone:=employee.clone()
employee.firstName:="MARIE"
employee.lastName:="SOPHIE"
employee.salary:=500
$diff1:=$clone.diff(employee) // All differences are returned
$diff2:=$clone.diff(employee;New collection("firstName";"lastName"))
// Only differences on firstName and lastName are returned
$diff1:
[
{
"attributeName": "firstName",
"value": "Natasha",
"otherValue": "MARIE"
},
{
"attributeName": "lastName",
"value": "Locke",
"otherValue": "SOPHIE"
},
{
"attributeName": "salary",
"value": 66600,
"otherValue": 500
}
]
$diff2:
[
{
"attributeName": "firstName",
"value": "Natasha",
"otherValue": "MARIE"
},
{
"attributeName": "lastName",
"value": "Locke",
"otherValue": "SOPHIE"
}
]
Example 2
var vCompareResult1; vCompareResult2; vCompareResult3; $attributesToInspect : Collection
vCompareResult1:=New collection
vCompareResult2:=New collection
vCompareResult3:=New collection
$attributesToInspect:=New collection
$e1:=ds.Employee.get(636)
$e2:=ds.Employee.get(636)
$e1.firstName:=$e1.firstName+" update"
$e1.lastName:=$e1.lastName+" update"
$c:=ds.Company.get(117)
$e1.employer:=$c
$e2.salary:=100
$attributesToInspect.push("firstName")
$attributesToInspect.push("lastName")
vCompareResult1:=$e1.diff($e2)
vCompareResult2:=$e1.diff($e2;$attributesToInspect)
vCompareResult3:=$e1.diff($e2;$e1.touchedAttributes())
vCompareResult1 (all differences are returned):
[
{
"attributeName": "firstName",
"value": "Karla update",
"otherValue": "Karla"
},
{
"attributeName": "lastName",
"value": "Marrero update",
"otherValue": "Marrero"
},
{
"attributeName": "salary",
"value": 33500,
"otherValue": 100
},
{
"attributeName": "employerID",
"value": 117,
"otherValue": 118
},
{
"attributeName": "employer",
"value": "[object Entity]",// Entity 117 from Company
"otherValue": "[object Entity]"// Entity 118 from Company
}
]
vCompareResult2 (only differences on $attributesToInspect are returned)
[
{
"attributeName": "firstName",
"value": "Karla update",
"otherValue": "Karla"
},
{
"attributeName": "lastName",
"value": "Marrero update",
"otherValue": "Marrero"
}
]
vCompareResult3 (only differences on $e1 touched attributes are returned)
[
{
"attributeName": "firstName",
"value": "Karla update",
"otherValue": "Karla"
},
{
"attributeName": "lastName",
"value": "Marrero update",
"otherValue": "Marrero"
},
{
"attributeName": "employerID",
"value": 117,
"otherValue": 118
},
{
"attributeName": "employer",
"value": "[object Entity]",// Entity 117 from Company
"otherValue": "[object Entity]"// Entity 118 from Company
}
]
.drop()
History
Release | Changes |
---|---|
17 | Added |
.drop( {mode : Integer} ) : Object
Parameter | Type | Description | |
---|---|---|---|
mode | Integer | -> | dk force drop if stamp changed : Forces the drop even if the stamp has changed |
Result | Object | <- | Result of drop operation |
Description
The .drop()
function deletes the data contained in the entity from the datastore, from the table related to its Dataclass. Note that the entity remains in memory.
In a multi-user or multi-process application, the .drop()
function is executed under an "optimistic lock" mechanism, wherein an internal locking stamp is automatically incremented each time the record is saved.
By default, if the mode parameter is omitted, the function will return an error (see below) if the same entity was modified (i.e. the stamp has changed) by another process or user in the meantime.
Otherwise, you can pass the dk force drop if stamp changed
option in the mode parameter: in this case, the entity is dropped even if the stamp has changed (and the primary key is still the same).
Result
The object returned by .drop( )
contains the following properties:
Property | Type | Description | |
---|---|---|---|
success | boolean | true if the drop action is successful, false otherwise. | |
Available only in case of error: | |||
status(*) | number | Error code, see below | |
statusText(*) | text | Description of the error, see below | |
Available only in case of pessimistic lock error: | |||
LockKindText | text | "Locked by record" | |
lockInfo | object | Information about the lock origin | |
task_id | number | Process id | |
user_name | text | Session user name on the machine | |
user4d_alias | text | User alias if defined by SET USER ALIAS , otherwise user name in the 4D directory | |
host_name | text | Machine name | |
task_name | text | Process name | |
client_version | text | ||
Available only in case of serious error (serious error can be trying to duplicate a primary key, disk full...): | |||
errors | collection of objects | ||
message | text | Error message | |
component signature | text | internal component signature (e.g. "dmbg" stands for the database component) | |
errCode | number | Error 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 entity does not exist anymore | 5 | The entity no longer exists in the data. This error can occur in the following cases: |
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)..save( ) : error only if the dk auto merge option is not used.drop( ) : error only if the dk force drop if stamp changed option is not used.lock( ) : error only if the dk reload if stamp changed option is not used |
dk status wrong permission | 1 | The current privileges do not allow the drop of the entity. Associated statusText: "Permission Error" |
Example 1
Example without dk force drop if stamp changed
option:
var $employees : cs.EmployeeSelection
var $employee : cs.EmployeeEntity
var $status : Object
$employees:=ds.Employee.query("lastName=:1";"Smith")
$employee:=$employees.first()
$status:=$employee.drop()
Case of
:($status.success)
ALERT("You have dropped "+$employee.firstName+" "+$employee.lastName) //The dropped entity remains in memory
:($status.status=dk status stamp has changed)
ALERT($status.statusText)
End case
Example 2
Example with dk force drop if stamp changed
option:
var $employees : cs.EmployeeSelection
var $employee : cs.EmployeeEntity
var $status : Object
$employees:=ds.Employee.query("lastName=:1";"Smith")
$employee:=$employees.first()
$status:=$employee.drop(dk force drop if stamp changed)
Case of
:($status.success)
ALERT("You have dropped "+$employee.firstName+" "+$employee.lastName) //The dropped entity remains in memory
:($status.status=dk status entity does not exist anymore)
ALERT($status.statusText)
End case
.first()
History
Release | Changes |
---|---|
17 | Added |
.first(): 4D.Entity
Parameter | Type | Description | |
---|---|---|---|
Result | 4D.Entity | <- | Reference to first entity of an entity selection (Null if not found) |
Description
The .first()
function returns a reference to the entity in first position of the entity selection which the entity belongs to.
If the entity does not belong to any existing entity selection (i.e. .getSelection( ) returns Null), the function returns a Null value.
Example
var $employees : cs.EmployeeSelection
var $employee; $firstEmployee : cs.EmployeeEntity
$employees:=ds.Employee.query("lastName = :1";"H@") //This entity selection contains 3 entities
$employee:=$employees[2]
$firstEmployee:=$employee.first() //$firstEmployee is the first entity of the $employees entity selection
.fromObject()
History
Release | Changes |
---|---|
17 | Added |
.fromObject( filler : Object )
Parameter | Type | Description | |
---|---|---|---|
filler | Object | -> | Object from which to fill the entity |
Description
The .fromObject()
function fills an entity with the filler content.
This function modifies the original entity.
The mapping between the object and the entity is done on the attribute names:
- If a property of the object does not exist in the dataclass, it is ignored.
- Data types must be equivalent. If there is a type mismatch between the object and dataclass, 4D tries to convert the data whenever possible (see
Converting data types
), otherwise the attribute is left untouched. - The primary key can be given as is or with a "__KEY" property (filled with the primary key value). If it does not already exist in the dataclass, the entity is created with the given value when .save() is called. If the primary key is not given, the entity is created and the primary key value is assigned with respect to database rules. The auto-increment is only computed if the primary key is null.
filler can handle a related entity under the following conditions:
- filler contains the foreign key itself, or
- filler contains a property object with the same name as the related entity, containing a single property named "__KEY".
- if the related entity does not exist, it is ignored.
Example
With the following $o object:
{
"firstName": "Mary",
"lastName": "Smith",
"salary": 36500,
"birthDate": "1958-10-27T00:00:00.000Z",
"woman": true,
"managerID": 411,// relatedEntity given with PK
"employerID": 20 // relatedEntity given with PK
}
The following code will create an entity with manager and employer related entities.
var $o : Object
var $entity : cs.EmpEntity
$entity:=ds.Emp.new()
$entity.fromObject($o)
$entity.save()
You could also use a related entity given as an object:
{
"firstName": "Marie",
"lastName": "Lechat",
"salary": 68400,
"birthDate": "1971-09-03T00:00:00.000Z",
"woman": false,
"employer": {// relatedEntity given as an object
"__KEY": "21"
},
"manager": {// relatedEntity given as an object
"__KEY": "411"
}
}
.getDataClass()
History
Release | Changes |
---|---|
17 R5 | Added |
.getDataClass() : 4D.DataClass
Parameter | Type | Description | |
---|---|---|---|
Result | 4D.DataClass | <- | DataClass object to which the entity belongs |
Description
The .getDataClass()
function returns the dataclass of the entity. This function is useful when writing generic code.
Example
The following generic code duplicates any entity:
//duplicate_entity method
//duplicate_entity($entity)
#DECLARE($entity : 4D.Entity)
var $entityNew : 4D.Entity
var $status : Object
$entityNew:=$entity.getDataClass().new() //create a new entity in the parent dataclass
$entityNew.fromObject($entity.toObject()) //get all attributes
$entityNew[$entity.getDataClass().getInfo().primaryKey]:=Null //reset the primary key
$status:=$entityNew.save() //save the duplicated entity
.getKey()
History
Release | Changes |
---|---|
17 | Added |
.getKey( { mode : Integer } ) : Text
.getKey( { mode : Integer } ) : Integer
Parameter | Type | Description | |
---|---|---|---|
mode | Integer | -> | dk key as string : primary key is returned as a string, no matter the primary key type |
Result | Text | <- | Value of the text primary key of the entity |
Result | Integer | <- | Value of the numeric primary key of the entity |
Description
The .getKey()
function returns the primary key value of the entity.
Primary keys can be numbers (Integer) or strings. You can "force" the returned primary key value to be a string, no matter the actual primary key type, by passing the dk key as string
option in the mode parameter.
Example
var $employees : cs.EmployeeSelection
var $employee : cs.EmployeeEntity
$employees:=ds.Employee.query("lastName=:1";"Smith")
$employee:=$employees[0]
ALERT("The primary key is "+$employee.getKey(dk key as string))
.getRemoteContextAttributes()
History
Release | Changes |
---|---|
19R5 | Added |
.getRemoteContextAttributes() : Text
Parameter | Type | Description | |
---|---|---|---|
result | Text | <- | Context attributes linked to the entity, separated by a comma |
Advanced mode: This function is intended for developers who need to customize ORDA default features for specific configurations. In most cases, you will not need to use it.
Description
The .getRemoteContextAttributes()
function returns information about the optimization context used by the entity .
If there is no optimization context for the entity, the function returns an empty Text.
Example
var $ds : 4D.DataStoreImplementation
var $address : cs.AddressEntity
var $p : cs.PersonsEntity
var $contextA : Object
var $info : Text
var $text : Text
$ds:=Open datastore(New object("hostname"; "www.myserver.com"); "myDS")
$contextA:=New object("context"; "contextA")
$address:=$ds.Address.get(1; $contextA)
$text:=""
For each ($p; $address.persons)
$text:=$p.firstname+" "+$p.lastname
End for each
$info:=$address.getRemoteContextAttributes()
//$info = "persons,persons.lastname,persons.firstname"
See also
EntitySelection.getRemoteContextAttributes()
.clearAllRemoteContexts()
.getRemoteContextInfo()
.getAllRemoteContexts()
.setRemoteContextInfo()