Parse.com PHP SDK获取对象的用户关系 [英] Parse.com PHP SDK Get User Relations for Object

查看:70
本文介绍了Parse.com PHP SDK获取对象的用户关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将用户喜欢"保存为与Post对象的关系,就像在文档中一样.

I'm saving user "Likes" as a relation to Post objects, just like in the docs.

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

到目前为止,这个方法工作正常.

This works fine so far.

现在,我想知道如何查询帖子,以及如何统计每个帖子喜欢多少用户.我不想为每个帖子循环运行单独的查询.

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

我还想知道CurrentUser是否也喜欢查询中的每个帖子.同样,绝对要避免循环进行多个查询.

I would also like to know if the CurrentUser likes each post in the query as well. Again, definitely want to avoid making multiple queries in a loop.

这可能吗?

我当前的帖子查询:

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

推荐答案

[...]提取每个帖子顶多少用户[[]]

[...] pull a Count of how many users liked each post [...]

在Post类中创建一个喜欢计数器字段,并在每次以喜欢"关系添加/删除帖子时对其进行更新.您会在多个地方找到此建议(例如,此处递增方法(带正号或负号)负值)以自动更新计数器.

Create a likes counter field in the Post class, and update it each time the Post is added/removed in a "likes" relation. You'll find this suggested in more than one place (for example, here and here).
Use Parse's increment method (with positive or negative value) to atomically update the counter.

[...]知道CurrentUser是否喜欢每个帖子[...]

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

如果没有多个查询,这很难.您知道让用户喜欢的帖子很容易:

This is hard without multiple queries. You know it's easy to get the posts liked by the user:

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

您还可以设法获得与用户喜欢"关系的不是的帖子.您可能会尝试使用这种信息,但是您必须接受脚本简化的简化以及可能的不准确性(请记住,单个Parse查询不能检索超过1000个结果).您可能会发现最好仅在单个帖子视图中将信息显示为详细信息(也可以在与单个帖子条目相关的输入事件时,或者根据视口内部条目的当前可见性,通过AJAX动态获取信息. ).

You may also manage to get the posts that are not in a "likes" relation with the user. You might try using this kind of information, but you'd have to accept a reduction of simplicity in your script, along with a possible level of inaccuracy (remember that you can't retrieve more than 1000 results with a single Parse query). You may find it preferable to only show the information as a detail in the single post view (the information could also be dynamically fetched via AJAX upon an input event related to the single post entry or depending on the current visibility of the entries inside the viewport).

这篇关于Parse.com PHP SDK获取对象的用户关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆