MODEL_LINK

A ModelLink field references (or "links to") another model. In the Bookstore example, SalesRep is a ModelLink field on Book which references the User model. By configuring a field to be a ModelLink, the database table has a column whose foreign key points to the surrogate id of the referenced table.

There are three different ways in which you can configure a ModelLink filter: single-select, multi-select and wild card.
images/download/attachments/144835578/mlTypes-version-1-modificationdate-1645046018000-api-v2.jpg

We will discuss all three filtering techniques below, as well as retrieval.

Single-Select, Exact-Match Filtering

The most common way of filtering using a ModelLink field is to allow the user to provide a single value in the ModelLink auto-complete/pop-up "picker" field. For example, given the following SQL:

and ${filterIfNotNull:VAR_SYS_USER_ID,users.sys_user_id = $VAR_SYS_USER_ID$}

We would provide the following filter definition:

<CustomFilterField>
<FieldRef levelType="ZBKS.Book" category="PDF">
<ModelLinkTargetLevelType>User</ModelLinkTargetLevelType>
<FieldName>Sales Representative</FieldName>
</FieldRef>
<Type>MODEL_LINK</Type>
<ModelLinkMapping surrogateIdSqlName="VAR_SYS_USER_ID" displayFieldSqlName="VAR_SALES_REP">
<AutoCompleteSql>
SELECT users.sys_user_id, users.user_name
FROM users
WHERE UPPER(users.user_name) LIKE UPPER ($TYPED_VALUE$) || '%'
</AutoCompleteSql>
</ModelLinkMapping>
</CustomFilterField>

Please note the following key points:


  • The FieldRef should contain a ModelLinkTargetLevelType which references the linked Model. In this case: User.

  • Type is set as MODEL_LINK.

  • ModelLinkMapping is defined, with mappings for the surrogateIdSqlName and displayFieldSqlName. You may choose to filter by either, but typically the surrogate id will be used, as the display name may not be globally unique.

  • An AutoCompleteSql may optionally be provided to override the default auto-complete behavior of the field. This is particularly useful when you want to apply additional implicit filtering of the linked model table. You can also override the pop-up report behavior, see the "Picker Report" section for more details.

Multi-Select, Exact-Match Filtering

You can configure your ModelLink filter to support multiple selection. Consider this example:

and ${filterIfNotNull:VAR_SYS_USER_ID,users.sys_user_id in $VAR_SYS_USER_ID$}
...
<ModelLinkMapping multiSelectMode="true" surrogateIdSqlName="VAR_SYS_USER_ID" displayFieldSqlName="VAR_SALES_REP"/>
...

By providing the attribute multiSelectMode="true", it will allow the user to select multiple Users in the ModelLink filter field, and by using the "in" keyword in our query we can ensure we join properly to the USERS table against all the supplied values.