Support Topics Documentation Slack YouTube Blog

How get albums and all related videos

Hello
I just need your suggestion on how to approach this.
I have a table of users and table of videos.
Now I want to create concept of Albums. Basically a user can have multiple albums and every album can have multiple videos. So I was thinking of the following:
Create Album table with the following fields:
-Owner: One-to-One mapping with User Table
-Title: String
-Videos: One-to-Many mapping with Video Table

I want to do one call to get all albums that user has and all videos belonging to each Album.

How can I achieve that? I am open to suggestion on changing the format of tables

Thank you

Hello, @snakeeyes

The described format of tables is correct.

To get all user’s albums and all videos belonging to each album, you should make a query request to Album table with specified user’s ID:

DataQueryBuilder dataQuery = DataQueryBuilder.create().setWhereClause("owner.objectId = '" + user.getObjectId() + "'");
Backendless.Data.of(Album.class).find(dataQuery)

The full example for Java:

public static void getAlbums() {
    BackendlessUser user;
    DataQueryBuilder dataQuery = DataQueryBuilder.create().setWhereClause("owner.objectId = '" + user.getObjectId() + "'");
    Backendless.Data.of(Album.class).find(dataQuery, new AsyncCallback<List<Album>>() {
        @Override
        public void handleResponse(List<Album> albums) {
            for (Album album : albums) {
                System.out.println("Title: " + album.getTitle());
                System.out.println("Videos count: " + album.getVideos().size());
            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            System.out.println("Error: " + fault.getMessage());
        }
    });
}

Don’t forget to enable autoload option for ‘owner’ and ‘videos’ columns in your Album table.

Best Regards,
Maksym

Hello Maksym
Thank you for your response

But wouldn’t that load a maximum of 12 related videos per album ?

Thanks

Yes, it has maximum limit. If you have a large number of objects (for example Video objects) you should fetch it with pagination. Here is documentation with example: Data Relation Paging
Also in this situation you can’t get all of the data with one call because there are maximum limits.

Best Regards, Maksym

Thank you
So if I have 20 videos and 5 albums

Then my first call will get me all the albums and 12 videos each

Then I would need to use pagination for each album which means I will have to do another 5 calls

Is my understanding correct ?

Thank you

It depends how many albums the user has and how many videos each album has.
If you don’t know exactly how many albums user has (he can create 1 or 100) and how many videos the albums contains, it’s better to use pagination.

  1. Use pagination to fetch all albums of user.
  2. Use pagination to fetch all videos for each album.

In this way you are guaranteed to receive all of the albums and videos and don’t skip any data.

Best Regards,
Maksym

Thank you so much for your answers

You are welcome, @snakeeyes. Happy coding with Backendless.