usage of reversed() expression in Mendix

0
Hi,   I am going through Mendix advanced certification course and I see following text in one of the chapters (https://academy.mendix.com/link/modules/373/lectures/3038/6.3.2-Querying-in-Reverse)    The [reversed ()] expression can only be applied on self-references. When an association is between two different object types, the platform will be able to determine the direction of the join automatically.   I am little bit confused, the 1st statement says reversed() expression is only applicable to self references. The second statement talks about association between two different object types. If the reversed() expression is only applicable to self reference, under what condition we can use the reversed expression for different object types? These two statements can also be read as two independent facts without connecting with each other, but then it makes me think why they are written together if that is the case? Can experts please clarify if I am missing something.   Thanks, Sameer
asked
3 answers
3

“The [reversed ()] expression can only be applied on self-references. When an association is between two different object types, the platform will be able to determine the direction of the join automatically.”

 

The first statement explains that reversed() can only be used in self references. The second statement explains why reversed() is not useful for references between different object types – there, the platform can always determine the direction of the association automatically, because there is a known owner of the association.

 

Imagine a reference LineItem → Order

Here, when traversing the association, the direction is clear.

 

Now imagine a self-reference of User

Which User is the owner? Each user may have a parent and/or a child, so you need to be able to explicitly tell it which way the association should be traversed.

answered
0

Hi Sameer!

IMO
We cannot use the [reversed()] expression in the context of two different object types, just self-references.

Kindly refer https://forum.mendix.com/link/space/app-development/questions/2882 as well.

And the end part of the doc Querying-in-Reverse refers to querying specialization objects with the help of a java action to get the parent object from the specialized object.

answered
0

Associations Using Self Reference in Mendix

Example: Player Entity -  "Sub-type Players": Apprentice and Buddy

Association Update:  Child (Owner) Updates: The association is always updated through the child. In this case, the Apprentice is the owner.

 

Structure of the Association

Association Name: Apprentice_Buddy 

Right Side (Parent): Buddy 

Left Side (Child/Owner): Apprentice

 

  • Searching for the Child

XPath Query:  [SoccerSquad.Apprentice_Buddy = '[%CurrentObject%]']  

 

Direction The XPath constraint is read from right to left. 

Result: This query returns the Apprentices associated with the Buddy that is the `[%CurrentObject%]`.

 

  • Searching for the Parent

Querying in Reverse

Purpose: When you have an Apprentice and want to retrieve its Buddy (Parent) from the database. Using [reversed()]: This expression tells Mendix to read the constraint in the reverse direction.  Restriction: The [reversed()] expression can only be applied to self-references.

 

XPath Query: [SoccerSquad.Apprentice_Buddy [reversed ()] = '[%CurrentObject%]']  

 

Objective: To find the Buddy (parent) associated with the Apprentice (child) represented by `[%CurrentObject%]`. 

Reversed Direction: The [reversed()] expression reverses the query direction, making it from the perspective of the Apprentice looking for its Buddy. 

Result: The query returns the Buddy associated with the current Apprentice (`[%CurrentObject%]`).

 

answered