“How can I know the dataClass of an entity? I need it to write generic methods“. “I need information about a field in a dataClass: what is its type? Is it indexed? Is it unique?“. These are the kinds of questions we’ve heard you asking on the forum. 4D v17 R5 provides the answers: introducing new ORDA member methods to provide useful information about your database. Keep reading, because you’ll appreciate the benefits of reducing the size of your code and making it reusable and easy to maintain!
HDI: Example of getting database structure information with ORDA
Now, you can get structure-related information such as table or field number. This can be helpful when integrating ORDA step-by-step into your classic 4D code. However, the main purpose of these member methods is to give you a way to write generic code that’s applicable no matter the database’s structure.
Get the dataClass of an entity or an entity selection
entity dataclass
What a joy to have a clever entity that knows its own dataClass!
The getDataClass() member method returns the dataClass object corresponding to the entity. Then, you can call any member method available on the dataClass.
In the example below, we call the new() member method. It creates a new entity from the one provided and copies the address data. An object containing the property name and its value for the newly created entity is also received.
C_OBJECT($1;$entity;$entityContent;$newEntity;$status)
C_OBJECT($2;$attribute)
$entity:=$1
$attribute:=$2
$newEntity:=$entity.getDataClass().new()
$entityContent:=$entity.toObject("address.*")
//We copy address from the received entity
$newEntity.fromObject($entityContent)
//We fill the created entity with received property name and value
$newEntity[$attribute.name]:=$attribute.value
$status:=$newEntity.save()
entity selection dataclass
The getDataClass() member method returns the dataClass object corresponding to the entity selection. The method below receives an entity selection and creates new entities with the same address data.
C_OBJECT($1;$inputSelection;$newEntitySelection)
C_COLLECTION($esContent)
$inputSelection:=$1
alert(String($inputSelection.length) + " entities are going to be duplicated")
$esContent:=$inputSelection.toCollection("address")
$newEntitySelection:=$inputSelection.getDataClass().fromCollection($esContent)
alert(String($newEntitySelection.length) + " entities have been created")
get information about a dataclass
The getInfo() member method available on the dataClass, also returns useful information. In the example below, we get IDs matching the primary key of the Persons dataClass, then we retrieve the corresponding entities.
C_OBJECT($es;$settings)
$settings:=New object
$settings.parameters:=New object("receivedIds";getIds ())
// The getIds() method returns a collection of IDs
$settings.attributes:=New object("pk";ds.Persons.getInfo().primaryKey)
$es:=ds.Persons.query(":pk in :receivedIds";$settings)
dataclass attribute
With 4D V17.0, you learned that each dataClass attribute is an object. They’ve now been enhanced with conceptual and structural information.
Here’s a quick example.
C_TEXT($text)
$text:=JSON Stringify(ds.Cats.ID)
//$text =
//{"name":"ID","kind":"storage","type":"number","indexed":true,
//"keywordIndexed":false,"autoFilled":true,"mandatory":true,"unique":true,
//"fieldNumber":1,"fieldType":9}
Download the HDI above to learn more! And be sure to check out the doc center.