4D Blog

Home Product ORDA: work with objects and collections

ORDA: work with objects and collections

April 16, 2018

Product

In this latest addition to the ORDA series, we’ll look at how the new ORDA concepts can interact with existing objects and collections. In fact, it’s possible to turn entities and entity selections into objects and collections, as well as build entities and entity selections from objects and collections. By doing so, you can easily integrate ORDA code within your existing code. You can take full advantage of ORDA without needing to rewrite your code!

Today you use SELECTION TO ARRAY and ARRAY TO SELECTION to work in a disconnected way from the server, but you have to manage as many arrays as fields. And for a current record, there is no way to extract it in any form. With ORDA, you can easily export entities and entity selections from the database to work on them separately. And that’s not all, you can import previously created objects and collections into the database.  All with a single and easy-to-use method!

This is very useful to work efficiently in client server mode, to communicate with other applications, or to import data using batch processes.

ORDA: working with objects and collections

EXPORTING ENTITY AND ENTITY SELECTIONs TO OBJECTs AND COLLECTIONs

The following examples are based on this structure. Let’s get our hands dirty!

EXPORT TO OBJECTs

An entity can be extracted as an object using the toObject() method. Here, we extract the $pupil entity as an object. We can choose which properties to extract.

Then, we can apply the JSON Stringify command to convert the value of this object into a JSON string and send it through the network, for example. C_OBJECT($pupil;$pupilObject)
C_TEXT($pupilString)
//Get the first pupil whose last name is "Dunaway"
$pupil:=ds.Pupil.query("lastName=:1";"Dunaway").first()
If ($pupil#Null)
// Turn the entity to an object
 $pupilObject:=$pupil.toObject("firstName, lastName, email")
 $pupilString:=JSON Stringify($pupilObject) // Stringify the object
End if

The $pupilObject object will be:

{
 "firstName": "Cecil",
 "lastName": "Dunaway",
 "email": "Cecil.Dunaway@echo"
}

EXPORT TO COLLECTIONs

An entity selection can be exported to a collection using the toCollection() method. Here, we export the $pupils entity selection (which contains 2 entities) as a collection. We decide to extract only the student’s lastName and firstName properties, along with all the properties of the school related entity.

Then, we can apply the JSON Stringify command to convert the values of this collection into a JSON string and send it through the network, for example.

C_OBJECT($pupils)
C_COLLECTION($pupilsCollection)
C_TEXT($pupilsString) 
// Get the pupils whose first name starts with "L" 
$pupils:=ds.Pupil.query("firstName=:1";"L@")
//Turn the entity selection to a collection of objects
$pupilsCollection:=$pupils.toCollection("lastName,firstName,school.*")
$pupilsString:=JSON Stringify($pupilsCollection)

The $pupilsCollection collection will be:

[
  {
   "firstName": "Lauren",
   "lastName": "Bouchard",
   "school": {
   "ID": 45,
   "name": "Arts schools",
   "state": "New Jersey" }
  },
  {
   "firstName": "Laurie",
   "lastName": "Valenti",
   "school": {
   "ID": 46,
   "name": "Mathematics school",
   "state": "New Jersey"}
  }
]

More options for toObject() and toCollection() methods are available, check out the documentation for more details!

IMPORTING ENTITy AND ENTITY SELECTIONs FROM OBJECTs AND COLLECTIONs

import from Objects

An entity can be built from an object with the fromObject() method. Here, we create a new $pupil entity from a given object:

C_OBJECT($pupil;$pupilObject)
$pupilObject:=New object("firstName";"Mary";"lastName";"Smith";"email";"Mary.Smith@whisky")
//Create a new empty entity
$pupil:=ds.Pupil.new()
//Fill entity
$pupil.fromObject(
$pupilObject)

import from collections

An entity selection can be built from a collection with the fromCollection() method. Here, we build a $pupils entity selection from a given collection.

Note that this mechanism can be used for creating new entities or updating existing ones. It simply depends on the primary key’s presence in the objects.

C_OBJECT($pupils)
C_TEXT($txtPupils)
C_COLLECTION($pupilsCollection)
$txtPupils:=Document to text(Get 4D folder(Current resources folder)+"pupils_data.json")
//Get a collection of objects
$pupilsCollection:=JSON Parse($txtPupils)
$pupils:=ds.Pupil.fromCollection($pupilsCollection)

and here is the $pupilsCollection collection:

[
 {             // The primary key of the entity is not given. A new entity will be created
 "firstName": "Mark",
 "lastName": "Mac Arthur",
 "email": "Mark.MacArthur@yankee",
 "school": {
 "__KEY": "45"
 }
},
 {          // The primary key of the entity is given. This entity will be updated (if it exists)
 "__KEY": 3436, 
 "firstName": "Victor",
 "lastName": "Valenti",
 "email": "Victor.Valenti@NEWSCHOOL",
 "school": {
 "__KEY": "45"
 }
}
 ]

 

Discuss

Tags Database, Objects, ORDA, v17

Latest related posts

  • November 14, 2025

    Event Report in 4D Qodly Pro: See Every Interactions at a Glance

  • November 14, 2025

    4D Qodly Pro: Page Zoom Controls

  • November 13, 2025

    macOS Tahoe, Windows 11, which operating systems for 4D 20 and 21?

Avatar
Marie-Sophie Landrieu-Yvert
- Product Owner - Marie-Sophie Landrieu-Yvert joined the 4D Product team as a Product Owner in 2017. In this role, she is responsible for writing user stories and translating them into functional specifications. She also ensures that the delivered feature implementation meets the customer's needs. Marie-Sophie graduated from the engineering school ESIGELEC and began her career as an engineer at IBM in 1995. She took part in various projects (maintenance and development projects) and worked as a COBOL developer. She then moved on to work as a UML designer and Java developer. More recently, her main responsibilities included analyzing and writing functional requirements, and coordinating business and development teams.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy