Reusing the same Express entity in multiple associations

Apr 11, 2024

Concrete CMS supports the reuse of Express Associations to create complex data structures. The key to accomplish this task is to ensure each association is named with a unique handles to avoid conflicts.

In this example, we will set up and test a data object named "Boardroom" with multiple associations to an "Atomik Employee" object.

Step 1: Define Your Data Objects

First, ensure you have at least two data objects to associate. In our example, we have "Boardroom" and "Employee".

  1. Atomik Employee Object: This object is created automatically by the Atomik theme when its documentation is installed. If the object cannot be found, go to Dashboard > Pages & Themes > Themes and select Create Documentation from the Atomik theme settings menu. We will use this object to represent the members of the boardroom.
  2. Boardroom Object: Go to Dashboard > Express and select Add Object. Name the object "Boardroom" with the handle boardroom and the plural handle boardrooms.

Step 2: Establish Associations

We will create multiple associations to the Employee object from the Boardroom object:

  1. Chairperson Association:

    • Add a new association and select "Atomik Employee" as the target object.
    • Choose the association type "Many to One".
    • Change the default Target Property Name from atomik_employee to chairperson by ticking the checkbox and editing the value. Although the default value is often sufficient, in this case, a unique property name describing the association must be set in order to allow for more than one association between the Boardroom and Atomik Employees object.
  2. Secretary Association:

    • Add another association and select "Atomik Employee" again.
    • Set the association type to "Many to One".
    • Name this association secretary.
  3. Members Association:

    • Add another association and select "Atomik Employee" again.
    • Set the association type to "One to Many".
    • Name this association members.

Step 3: Add the associations to a forms

  1. Make note of the order of the associations in the Boardroom object's Associations section. If it's not clear what the name of each association is, select each one to see the details. This is important because the target property name is not shown in this view nor in the edit form view. In the screenshot below, the first association is secretary, followed by members, and then chairperson.
  2. Go to the Boardroom object's Forms section and create or edit a form for the Boardroom.
  3. Add form controls to select the associations created. The order displayed in this view is the same as that shown in the first step above.
  4. As you add each association to the form, add a Custom Label to clearly differentiate the form controls in the UI.

Step 4: Testing

  1. Navigate to the Boardroom entries.
  2. Test creating a Boardroom record by selecting an Employee for the Chairperson and other Employees for Members:
  3. Save the form and verify that the associations are correctly listed in the Boardroom's details page:

Step 5: Programmatic Access

Developers can access these associations in custom code through the use of Magic Methods on the object. For example, getChairperson() accesses the chairperson association and getMembers() access the members association, both of which are Atomik Employee records.

Step 6: Debugging and Validation

If you encounter issues: - Check that each association has a unique handle. - Verify that the form controls are properly configured and saved. - Use logging and debugging tools to trace any errors during the association process.

Recent Tutorials
Customize locale icons
Oct 29, 2024
By myq.

How to customize locale (language region) flags

Concrete CMS Caching Guide
Oct 16, 2024

An overview of types of caching in Concrete and considerations when using them.

Redirect all requests to HTTPS
Oct 9, 2024
By myq.

How to follow best practices for a secure web

Upgrade Concrete versions 9.3.1 and 9.3.2
Sep 10, 2024
By myq.

How to get past a bug in versions 9.3.1 and 9.3.2 that prevents upgrading the Concrete core through the Dashboard

How to use Composer with Marketplace extensions
Aug 22, 2024

Composer can be used to manage third-party extensions from the marketplace

Controlling Google Tag Manager Tags Based on Concrete CMS Edit Toolbar Visibility
Aug 13, 2024

This document provides a step-by-step guide on how to control the firing of Google Tag Manager (GTM) tags based on the visibility of the Concrete CMS edit toolbar. It explains how to create a custom JavaScript variable in GTM to detect whether the edit toolbar is present on a page and how to set up a trigger that ensures GTM tags only fire when the toolbar is not visible. This setup is particularly useful for developers and marketers who want to ensure that tracking and analytics tags are not activated during content editing sessions, thereby preserving the accuracy of data collected.

Improvements?

Let us know by posting here.