iossqlbackendbackendless

Backendless iOS: intermediate table between two tables


I am new in Backendless and I have read all the manuals about relations, but still not sure how to create san intermediate table between two tables. For instance, I have table called users and tables called Events. A user can subscribe to events. So I want new table UserEvents, which has user_id and event_id. Also, how would I retrieve all events added by user? In other words, how to do joins in Backendless? (I suppose there is no joins and everything much simpler though).


Solution

  • There are two separate questions at hand here:

    1. How to create relationships between objects?
    2. How to load objects created by a user?

    Let's start with the first one:

    How to create relationships between objects?

    When you work with Backendless it is important to think in terms of objects and not tables. For example if there is an entity called Order and it contains a collection of OrderItem objects, then THAT is your data structure. You do not need to pre-create tables in console – Backendless will do it for you the very first time you save an Order object which has a collection of OrderItems. However, if you would like to do it by hand in our Console, here are the steps:

    1. Login to console, select an app, click on Data
    2. Create table Order (it is better to name tables in the singular form - so Order, not Orders).
    3. When you create a new table (click the "+" button in the lower-left corner), console will prompt you to switch to Schema Editor so you can add some data columns. A column would correspond to a property in the class which represents a record from the table.
    4. Now that the Order table is in place, repeat the process for the OrderItem table.
    5. Once both tables exist, we need to "link" the tables together. That link would establish a relationship which can be either one-to-one or one-to-many. To do this, select the Order table and click the Table Schema and Permissions red button in the upper right corner.
    6. Click the Add Relationship button.
    7. In the popup that appears, you will need to create a property which will contain a collection of order item objects. Name that property "orderItems" (it is okay to make it plural here). On the right side of the popup select the OrderItem table. In the Multiplicity drop-down select Many.
    8. Click Save. At this point the relationship is established.

    To see it working you can either use the code generation module which will give you all the source code for working with the tables. Click the Code Generation icon. In the Android section, select either Eclipse or IDEA in the IDE block, then click the Java classes for defined data tables option. Click Download Project. Backendless will generate a ZIP file with the source code for the client-side classes that will let you perform a full CRUD (Create, Retrieve, Update, Delete) range of operations on your tables.

    The documentation describes how to work with related data. For instance, see the approaches and API for retrieving related objects.

    Things are simpler when it comes to the question of how to load objects created by a user. So we come to the second question:

    How to load objects created by a user?

    The approach described above works equally well for linking the built-in Users table with any other table. However, specifically with the Users table, that link/relationship is not necessary. The reason for this is Backendless automatically tags any created object with the ID of the "owner", that is the user who created the object. That "tag" can be seen with the special ownerId property in the Users table. To enable retrieval of the objects which belong to the user, we need to modify the security permissions for the table which contains the objects created by users:

    1. Login to console, select your app and click the Data icon.
    2. Select the table with the objects created by users.
    3. Click the Table Security and Permissions red button in the upper right corner of the screen.
    4. Click the Role Permissions menu item.
    5. Locate the cell at the intersection of the Find column and the AuthenticatedUser row. Click the cell until you see a red cross.
    6. Repeat the previous step for the intersection of the Find column and the NotAuthenticatedUser row.

    At this point you restricted all access to your table for both authenticated and not-authenticated (guest) users. The next step will allow the owners to retrieve the objects which they created.

    1. Click the Owner Policy menu item.
    2. Click the cell in the Find column until there is a green check mark.
    3. At this point when you call any of the Find methods on that table, Backendless will return only the objects which belong to the currently logged in user.