0

I am building an Android app using Back4App(Parse) database. I have two classes Post and Post_likes. I want to fetch all posts listing from Post table with individual post likes from post_likes table in the same query.

In Post table, I have post details like post_id, user_id and other details. In Post_likes table, I have user_id who liked the particular post corresponding to post_id.

Post table with post details

Post_likes table with Post_id and user_id

Now, I want to get total post likes of corresponding post and users who like the particular post along with post listing.

I want to fetch total likes of particular post and then show in home screen post listing.

So, how to write the query to combine the two tables.

Any help will be deeply appreciated.

Ankit Kamboj
  • 59
  • 2
  • 9
  • What is your Relationship between Post and Post_Likes ? oneToOne, oneToMany ? If it's oneToOne you can simply include the Pointer in your query – ThierryC Mar 17 '17 at 13:10
  • Its one to may relation. In post table, i have multiple post_id. So, i want to select all likes from post_like table based on individual post_id. – Ankit Kamboj Mar 18 '17 at 07:32
  • I want to know how to make pointers between back4App classes to fetch data from multiple classes. – Ankit Kamboj Mar 18 '17 at 08:08

1 Answers1

1

To define a Pointer Column in the DB, From the Back4App's dashboard you can click on "Edit" button for one Class and add a new Column. Select the type "Pointer" and select the Class name you want to point to in the combo list.
In your code simple add the pointer in the corresponding field (Something like:

ParseObject  PostLikes aPostLike;
ParseObject Post thepost
aPostLike.put("pointedPost", thepost);

(See documentation for accurate syntax).

When you query List of PostLike you can add a "where clause" directly on the pointer Field "pointedPost".
You can also include the content of the Post while query all PostLikes with the key words "include": (see doc here : http://parseplatform.org/docs/android/guide/#relational-queries ) :

ParseQuery<ParseObject> query = ParseQuery.getQuery("PostLikes"); 
// Include the post data with each comment
query.include("post");
ThierryC
  • 1,794
  • 3
  • 19
  • 34