Quick search:

How to add relationship navigation

The easiest way to get navigation over a relationship is to use the default behavior of derived properties and multi value properties.

For adding navigation over an n to 1 relationship, add a derived property whose type is the class of the related peanuts (How to add a property). In order to work the derived property needs a property on the same class holding a foreign key. The name of this property must be the same as the derived property, extended with 'Id'. If this  property is 0 or an empty string, the derived property will return null. Otherwise it will attempt to retrieve a peanut of its type whose 'id' property is equal to the foreign key.

If you do not make the derived property readOnly, its value can also be set. It will get the id from the to be related peanut and set that as the value of the foreign key property.

Relationships are not limited to persistent peanuts. However, if the user should be able to set the value of a derived property and retrieve it later, you need a way to store the value of the foreign key. Therefore the foreign key property will usually be a persistent field property and the peanut that holds it will be persistent.

For adding navigation over a 1 to n relationship, add a multi value property whose type is the class of the related peanuts. In order to work the multi value property needs a property on the related class holding a foreign key. The name of this property must be the same as the lcFirst of the name of the class with the multi value property property, extended with 'Id'. A peanuts multi value property will retrieve all peanuts of the properties type whose foreign key property is equal to its own id.

For adding navigation over an m to n relationship (version 1.2 or higher), first add navigation over a 1 to n relationship. This will navigate to a relationship class, that has two persistent fieldProperties holding foreing keys, and two derived properties using the foreig keys to navigate over two n to 1 relatioships, one leading back to the object our navigation was starting from, the other further to the other side of the m to n relationship. Finally add a multi value property for navigating the m to n realtionship itself, and set its derivationPath to a String with the name of the first property and the name of the second property, separated by a dot. See example13.Keyword::initPropertyDescriptors for an example.

Navigation using default property behavior can be both ways (bi-directional) if the name of the property for navigating from the n to one is the same as the lcFirst of the name of the related type.

Multi value properties do not offer a way for their values to be set. If you need to 'add a peanut to the values of a multi value property, you may make the navigation bi-directional and use the property at the other side, or set the foreign key property directly. An exception are multi value properties for navigation over m to n relationships: These can be edited through an MtoNpropertyPage and a SaveAction, wich will call Ids of added and removed values are passed to a method MtoNmodIds, or if the method does not exist, to the propertyDescriptor. See example 13.

Property getters and setters may override all this. For example, if you need the name of the foreign key property for a multi value property to be different, you can make a getter that uses a foreign key property with a different name. Also see how to retrieve instances of a class from the database.