Parse.com PHP SDK Get user relationship for an object

I am storing user "Likes" as a relation to Post objects, as in docs.

$user = ParseUser::getCurrentUser();    
$post= new ParseObject("Post", $the_post_id);
$relation = $user->getRelation("likes");
$relation->add($post);
$user->save();

      

So far, that's fine.

Now I'm wondering how to query for posts and also pull the number of how many users liked each post. I don't want to run separate queries in a loop for each post.

I would also like to know if the CurrentUser likes each message in the request. Again, I definitely want to avoid creating multiple queries in a loop.

Is it possible?

My current request:

$query = new ParseQuery("Post");
$posts = $query->find();

      

+3


source to share


1 answer


[...] pull the number of how many users liked each post [...]

Create a "like" column in the Post class and update it every time a post is added / removed in the "likes" relationship. You will find this in more than one place (for example, here and here ).
Use the Parse increment method (positive or negative) to atomically update the counter.

[...] know if CurrentUser likes each post [...]



It is difficult without a few requests. You know it's easy to get messages that a user likes:

$postsLiked = $relation->getQuery()->find();

      

You will also manage to receive messages that do not match the relationship with the user. You can try using this information, but you'll have to accept the simplification in the script as well as the possible level of imprecision (remember, you can't get more than 1000 results with a single Parse query). You may prefer to only show the information as a separate message (information can also be dynamically loaded via AJAX on an input event associated with a single record, or depending on the current visibility of the records inside the viewport).

+5


source







All Articles