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

Home

 
4D v19.8
entity.fromObject( )

entity.fromObject( ) 


 

entity.fromObject ( object ) 
Parameter Type   Description
object  Object in Object from which to fill the entity

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.

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.

 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"
    }
}



See also 

entity.toObject( )

 
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)