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

Home

 
4D v19.8
entity.toObject( )

entity.toObject( ) 


 

entity.toObject ( filter {; options} ) -> Result 
Parameter Type   Description
filter  String, Collection in Attribute(s) to extract
options  Longint in dk with primary key: adds the _KEY property;
dk with stamp: adds the _STAMP property
Result  Object in Object built from the entity

The entity.toObject( ) method returns an object which has been built from the entity. Property names in the object match attribute names of the entity.

In the filter parameter, you pass the entity attribute(s) to extract. Two syntaxes are allowed:

  • a string with property paths separated with commas: "propertyPath1, propertyPath2, ...".
  • a collection of strings: ["propertyPath1","propertyPath2";...]

If filter is specified for attributes of the relatedEntity kind:

  • propertyPath = "relatedEntity" -> it is extracted with simple form: an object with property __KEY (primary key).
  • propertyPath = "relatedEntity.*" -> all the properties are extracted
  • propertyPath = "relatedEntity.propertyName1; relatedEntity.propertyName2; ..." -> only those properties are extracted

If filter is specified for attributes of the relatedEntities kind:

  • propertyPath = "relatedEntities.*" -> all the properties are extracted
  • propertyPath = "relatedEntities.propertyName1; relatedEntities.propertyName2; ..." -> only those properties are extracted

If the filter parameter contains an empty string or "*", the returned object will contain:

  • all storage entity attributes
  • attributes of the relatedEntity kind: you get a property with the same name as the related entity (name of the many-to-one link). Attribute is extracted with the simple form.
  • attributes of the relatedEntities kind: attribute is not returned.

In the options parameter, you can pass the dk with primary key and/or dk with stamp selector(s) to add the entity's primary keys and/or stamps in extracted objects.

The following structure will be used throughout all examples of this section:

Without filter parameter:

 employeeObject:=employeeSelected.toObject()

Returns:

{
    "ID": 413,
    "firstName": "Greg",
    "lastName": "Wahl",
    "salary": 0,
    "birthDate": "1963-02-01T00:00:00.000Z",
    "woman": false,
    "managerID": 412,
    "employerID": 20,
    "photo": "[object Picture]",
    "extra": null,
    "employer": { // relatedEntity extracted with simple form
        "__KEY": 20
    },
    "manager": {
        "__KEY": 412
    }
}

Extracting the primary key and the stamp:

 employeeObject:=employeeSelected.toObject("";dk with primary key+dk with stamp)

Returns:

{
    "__KEY": 413,
   "__STAMP": 1,
    "ID": 413,
    "firstName": "Greg",
    "lastName": "Wahl",
    "salary": 0,
    "birthDate": "1963-02-01T00:00:00.000Z",
    "woman": false,
    "managerID": 412,
    "employerID": 20,
    "photo": "[object Picture]",
    "extra": null,
    "employer": {
        "__KEY": 20
    },
    "manager": {
        "__KEY": 412
    }
}

Expanding all the properties of relatedEntities:

 employeeObject:=employeeSelected.toObject("directReports.*")

{
    "directReports": [
        {
            "ID": 418,
            "firstName": "Lorena",
            "lastName": "Boothe",
            "salary": 44800,
            "birthDate": "1970-10-02T00:00:00.000Z",
            "woman": true,
            "managerID": 413,
            "employerID": 20,
            "photo": "[object Picture]",
            "extra": null,
            "employer": {
                "__KEY": 20
            },
            "manager": {
                "__KEY": 413
            }
        },
        {
            "ID": 419,
            "firstName": "Drew",
            "lastName": "Caudill",
            "salary": 41000,
            "birthDate": "2030-01-12T00:00:00.000Z",
            "woman": false,
            "managerID": 413,
            "employerID": 20,
            "photo": "[object Picture]",
            "extra": null,
            "employer": {
                "__KEY": 20
            },
            "manager": {
                "__KEY": 413
            }
        },
        {
            "ID": 420,
            "firstName": "Nathan",
            "lastName": "Gomes",
            "salary": 46300,
            "birthDate": "2010-05-29T00:00:00.000Z",
            "woman": false,
            "managerID": 413,
            "employerID": 20,
            "photo": "[object Picture]",
            "extra": null,
            "employer": {
                "__KEY": 20
            },
            "manager": {
                "__KEY": 413
            }
        }
    ]
}

Extracting some properties of relatedEntities:

 employeeObject:=employeeSelected.toObject("firstName, directReports.lastName")

Returns:

{
    "firstName": "Greg",
    "directReports": [
        {
            "lastName": "Boothe"
        },
        {
            "lastName": "Caudill"
        },
        {
            "lastName": "Gomes"
        }
    ]
}

Extracting a relatedEntity with simple form:

 $coll:=New collection("firstName";"employer")
 employeeObject:=employeeSelected.toObject($coll)

Returns:

{
    "firstName": "Greg",
    "employer": {
        "__KEY": 20
    }
}

Extracting all the properties of a relatedEntity:

 employeeObject:=employeeSelected.toObject("employer.*")

Returns:

{
    "employer": {
        "ID": 20,
        "name": "India Astral Secretary",
        "creationDate": "1984-08-25T00:00:00.000Z",
        "revenues": 12000000,
        "extra": null
    }
}

Extracting some properties of a relatedEntity:

 $col:=New collection
 $col.push("employer.name")
 $col.push("employer.revenues")
 employeeObject:=employeeSelected.toObject($col)

Returns:

{
    "employer": {
        "name": "India Astral Secretary",
        "revenues": 12000000
    }
}



See also 

entity.fromObject( )

 
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)