We can all agree that ORDA is a great 4D V17 feature. A kind of revolution in the way of thinking and programming. 4D databases will never be seen the same way as before.
ORDA allows you to conceive advanced applications in less time, with more readable and clean code. To show you the power of ORDA we’ve prepared a series of tips for you, divided into three episodes. In this first episode, we’ll demonstrate how links should now be considered more as entities and entity collections, and we’ll show you how listboxes can now be based on entity selections, rather than selections or arrays.
Using recursive relationships
In order to demonstrate the power of 4D and ORDA, we’ll use a highly recursive database using a single table named PEOPLE.
This table contains five main fields: Lastname, Firstname, Birthday, Gender and ID (which is obviously unique).
Then come three more fields: FatherID, MotherID and PartnerID that connects each person to three other people if they exist (Partner) or if they are known (Mother and Father).
Now let’s focus on the relationship definition. In both directions (1 to N and N to 1), relationships can be named. Using ORDA, these names are going to become very significant and useful. In the many to one direction, the name is not only a string, it is an entity of the PEOPLE table, in the same way as the PEOPLE entity where the relationship originates. In the one to many direction it’s even more, the name represents an entity selection.
Without anticipating too much, you can guess that with the object notation, you’ll be able to use PEOPLE.Father, as well as PEOPLE.ChildrenAsFather, to reach either a single person (the father) or a group of persons (the children).
A new kind of listbox
Until now, the contents of list boxes (the data source) were either arrays or selections. From now on, the contents can also come from a collection or an entity selection. So let’s see how an entity selection can be used within a list box.
First of all, you have to create the entity selection using the new ds function. In this example, we’ll create a simple entity selection containing all of PEOPLE ordered by birthday, last name, and first name. This can be done in a single line, as shown below. That’s all you have to know for now about ds!
people:= ds.PEOPLE.all().orderBy("Birthday asc, Lastname asc, Firstname asc")
Once you get the people entity selection, just set it as a property of the list box and you’re – almost – done!
“THIS” is fantastic
As in regular list boxes, the contents of every column must be defined. In our case, as long as we use an entity selection, the contents of each column will be defined as an expression. To do this, we’ll use a new keyword (“This“) which will refer to the the current item of the entity selection.
To display people names and birthdays, the expression can be as simple as:
This.Lastname + " " +This.Firstname
Now comes the power and the fun: if you want to display the Lastname of the Father, the Birthday of the Mother, or the Firstname of the Partner, just use the entity names you gave in the relationship definitions (Father, Mother, Partner, etc.) and write:
Of course, you can go even further if you want to know the names of the four grandparents of the Partner of the current people by writing:
One last step (for today!): I also want to display the number of children and siblings this person has. Can I do that ?
Sure, that’s quite easy. For the children, just use the ChildrenAsFather entity selection defined in the relationship definition. As with every entity selection, it has a length property. Simply define the expression like this:
This.ChildrenAsFather.length + This.ChildrenAsMother.length // one of them will equal zero, ChildrenAsFather.length for women and vice versa.
For the siblings you’ll need to “go up” to the mother (or father) and “go down” to the children. So the expression would be :
This.Mother.ChildrenAsMother.length - 1 // minus 1 to remove the current people from the number of his/her siblings :-)
And here’s the result… without a single line of code !
To be continued, stay tuned!