Backendless Support
 
Solved

Retrieving last object from a table with a 1:many relation with the user table

My users table has a one to many relations with table "B".

How to get the last object in table "B" for the logged in user?

I can't seem to find how to use "findLast" directly on the related table "B".

should

  1. public void Backendless.Persistence.of("B").findLast(Asyncallback<Map> responder)

give me the desired result for the logged in user? It's not.

When should you use

  1. Backendless.Persistence.of

instead of

  1. Backendless.Data.of

when doing object retrieval?

Leave a Comment

Comments (8)

photo
1

You could do a search with whereClause. When you say find last "for the logged in user", could you elaborate what you mean by that? For example, is it the last updated object by the current user? or is it the last created object? or what exactly?

photo
1

The last created object in the related table, i have a data form that has texts and images, i don't want the images to go into my file system before the text entries are captured in the related table for obvious reasons, so the created entry has to wait for the File System to return the image urls, which i then want to add to that particular entry.

photo
1

I'd use the following query:

Users[relationNameToB].objectId = 'objectId-of-the-current-user'

Make sure to:

  1. set the pageSize to 1
  2. set the sortBy to "created DESC

With pageSize set to 1 the result will be a collection of 1 object, that's the last one created by the specified user.

Regards,

Mark

photo
1

Thanks Mark, but is that for Android? I never came across the above queries. If yes, could provide a link to the relevant documentation so i can pore through to implement your idea?

Thanks

George

photo
1
photo
1

Very helpful, just one thing

I have done this so far

  1. String whereClause = "Users[relationNameToB]" +
  2. ".objectId='" + user.getObjectId() + "'";
  3. DataQueryBuilder queryBuilder = DataQueryBuilder.create();
  4. queryBuilder.setWhereClause(whereClause);
  5. queryBuilder.setPageSize(1);
  6. queryBuilder.setSortBy("created DESC");

Do i now make the query as

  1. Backendless.Data.of(BackendlessUser.class).find(queryBuilder, new AsyncCallback<List<BackendlessUser>>(){
  2. }

Which looks like it will give me a list of users.....

or

  1. Backendless.Data.of(B.class).find(queryBuilder, new AsyncCallback<List<B>>(){
  2. }

to give me the last created object in the related table....

photo
1

You need to send the query to the table where you want to retrieve the data from. In this case, it is table "B"

photo
1

Thank you, appreciated.

photo