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). Thank you very much!

Hi Ibragim,

Thank you for the questions, you touched on some of the most interesting features we have in Backendless. There are two separate questions at hand here:

    How to create relationships between objects? 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:

    Login to console, select an app, click on Data Create table Order (it is better to name tables in the singular form - so Order, not Orders). 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. Now that the Order table is in place, repeat the process for the OrderItem table. 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. Click the Add Relationship button. 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. 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:

    Login to console, select your app and click the Data icon. Select the table with the objects created by users. Click the Table Security and Permissions red button in the upper right corner of the screen. Click the Role Permissions menu item. Locate the cell at the intersection of the Find column and the AuthenticatedUser row. Click the cell until you see a red cross. 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.

    Click the Owner Policy menu item.
    Click the cell in the Find column until there is a green check mark.
    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.

Enjoy!