The entity.fromObject( ) method fills an entity with the object content.
Note: This method 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 RELATE MANY), 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 entity.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.
object can handle a related entity under the following conditions:
- object contains the foreign key itself, or
- object 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.
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.
C_OBJECT($o)
$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"
}
}