This how-to explains how you can create a basic data layer for your application with Mendix. Each application can have multiple modules, and each module has its own domain model. All the domain models together define the data layer of the application.
The domain model consists of entities and associations. An entity is the blueprint for an object in your application, like “Customer” or “Order.” Associations define the relation between two entities. As soon as you deploy the application, Mendix takes care of the underlying database for you. This means that you won’t have to create tables and write queries yourself.
This how-to will teach you how to do the following:
- Create entities and attributes
- Add enumerations
- Create associations
- Delete association behavior
Before starting this how-to, make sure you have completed the following prerequisites:
- Download and install the Mendix Desktop Modeler
- Create a new app or have an existing app project available
3 Creating Entities and Attributes
To create entities and their attributes, follow these steps:
Open the Domain Model:
Click Entity in the menu bar:
Click inside the domain model editor to create the entity:
By default, the Modeler creates a persistent entity, which means that the app’s database will be able to store objects of this type of entity.
Start typing directly to change the name of the entity into Customer:
Right-click the Customer entity and select Add > Attribute:
Enter Name for the Name of the new attribute, and select String as the data Type:
Repeat the steps above to create a complete entity that looks like this:
Repeat the steps above again to create a second entity that looks like this:
4 Adding Enumerations
An enumeration is a predefined list of values that can be used as an attribute type. This allows users of the app to select any of the predefined values for this attribute. A good example of an enumeration is order status. Let’s add an enumeration and extend the Order entity with an enumeration value-based attribute.
To add enumerations, follow these steps:
- Right-click the module and select Add > Enumeration.
- Enter OrderStatus for the Name and click OK.
Click New to add a new enumeration value:
Enter Open for the Caption and click OK.
Repeat the steps above for the Processing and Complete values. You should then have the following configured values:
Click OK to save the enumeration. Now we will create an enumeration value-based attribute in the Order entity.
Right-click the Order entity and select Add > Attribute.
Enter OrderStatus for Name and select Enumeration for Type.
Select the OrderStatus enumeration and click Select.
Select Open for Default value:
Click OK to save the new attribute. The Order entity should look like this:
5 Creating Associations
After you have created the entities, you can start creating associations.
To create an association, draw a line from the border of one entity to the border of the other entity to create an association. Always start at the entity that will have more instances in the system than the other one. In this case, draw an association from Order to Customer, because you will want to store more orders than customers in your application.
This section will explain how to change the multiplicity of associations.
By default, the domain model editor creates an association with a one-to-many multiplicity. In the above case, a customer can have multiple orders, and an order can only have one customer. If the desired multiplicity is not available in the properties list you have probably drawn the association the wrong way, so you should remove the association and draw it again the other way around.
To change the multiplicity, double-click the Order_Customer association in order to open its Properties dialog box:
To change the association to a 1-to-1 multiplicity, select the [1 – 1] option in the Multiplicity section
- This means that a customer can only have one order and vice versa:
To change the association to a many-to-many multiplicity, select the [* – *] option in the Multiplicity section
- This means that a customer can have multiple orders and an order can have multiple customers:
7 Delete Behavior
You can configure the delete behavior for both sides of an association.
To configure the delete behavior, double-click the Order_Customer association to open its Properties dialog box:
To configure a cascading delete, select the Delete ‘Order’ object(s) as well option in the On delete of ‘Customer’ object section
- This means that all the orders of a customer will also be removed if the customer is deleted:
To configure the delete prevention, select the Delete ‘Customer’ object only if it is not associated with ‘Order’ object(s) in the On delete of ‘Customer’ object section
- This means that a customer can only be deleted if no orders refer to this customer
- The Error message will be shown to a user that tries to delete a customer that has orders
8 Related Content
- How to Work with Images & Files
- How to Denormalize Data to Improve Performance
- How to Set Up Data Validation
- How to Work with Object Events
- How to Find the Root Cause of Runtime Errors
- How to Define Access Rules Using XPath
- How to Perform the Scout and Windows 10 Workaround
- How to Create Your First Two Overview & Detail Pages