KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Get All Relational Attributes from Table
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: October 8, 2018

Below is a utility method to return all relation attributes of a table and whether that attribute is of Many-to-One or One-to-Many type.

/////////////////////////////////////////////////////////////////////
// Name: UTIL_GET_RELATED_FIELDS
//
// Description: Method takes pointer to table and returns an object of
// related attributes
//
// Input Parameters:
// $1 (POINTER) - Pointer to table
// Output:
// $0 (OBJECT) - Object that shows relational attribute name and type of relation
/////////////////////////////////////////////////////////////////////

C_OBJECT($emp;$0)
C_TEXT($1;$fieldName;$fieldType;$tableName)

If (Count parameters=1)
   $tableName:=Table name($1)
   $emp:=ds[$tableName].new()
  
   If ($emp#Null)
       For each ($fieldName;$emp)
         $fieldType:=ds[$tableName][$fieldName].kind
         If ($fieldType#"storage")
           OB SET($0;$fieldName;ds[$tableName][$fieldName].kind)
         End if
       End for each
   End if
  
End if


With the given structure below, we'll find all the related attributes for the Employee table.



Here is the result after finding the related attributes for the Employee table. "relatedEntity" represents Many-to-One while "relatedEntities" represents One-to-Many. Since many employees can belong to one company, the object correctly shows attribute "company" with type "relatedEntity". Likewise, employees can own many pets and the object shows attribute "pet" with "relatedEntities" property.