解析:当我只有PFUser时,如何使用PFRelation查询? [英] Parse: How do I query using PFRelation when I just have PFUser?

查看:115
本文介绍了解析:当我只有PFUser时,如何使用PFRelation查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要做的事的一个例子.我有当前用户为PFUser,在另一个名为Item的类上,我有一个名为所有者"的关系,它是PFUser的关系.我想查询在关系中具有当前用户的所有Item实例.

Here is an example of what I am trying to do. I have the current user as PFUser and on another class named Item I have a relation named "owners" which is a relation of PFUser. I want to query for all instances of Item which have the current user in the relation.

我看到了相反查询方式的示例,但是我没有看到如何获取关系,然后将其过滤为关系上的PFUser匹配项.这是一个例子.

I see examples of the opposite way of querying, but I do not see how to get a relation and then filter it to a PFUser match on the relation. Here is one example.

https://www.parse.com/questions/inverse-relationship-support

PFObject老师= ...////老师"类的PFObject PFRelation studentsRelation = [教师关系forKey:@"students"]; PFQuery * query = studentsRelation.query; [查询findObjectsInBackground:...

PFObject teacher = ... // PFObject of class "Teacher" PFRelation studentsRelation = [teacher relationforKey:@"students"]; PFQuery *query = studentsRelation.query; [query findObjectsInBackground:...

对于我的示例,我会这样做...

For my example I would do this...

PFObject item = [PFObject objectWithClassName:@"Item"];
PFRelation relation = [parseObject relationforKey:@"owner"]; // Filter to PFUser? 
[query findObjectsInBackground:...

我可以使用whereKey,但是我应该使用什么呢?我可以在objectId上匹配吗?

I can use whereKey, but what do I use? Do I match on objectId?

[query whereKey:@"objectId" equalTo:user.objectId];

我不断收到错误102:RelatedTo运算符需要一个有效的指针.我的猜测是,由于我是从一个空对象开始的,所以没有起点.使用Teacher的示例必须从填充的实例开始.我唯一填充的是PFUser.必须有一种方法来查询Item实例,并根据它与当前用户所拥有的所有者"关系对其进行过滤.我无法弄清楚,也没有找到如何执行此查询的示例.

I keep getting Error 102: a valid pointer is needed for RelatedTo operator. My guess is that since I am starting with an empty object it has no starting point. The example with Teacher must start with a populated instance. The only thing that I have that is populated is PFUser. There has to be a way to query for the Item instances and filter it on the "owners" relation that it owns to the current user. I cannot figure it out and I have not found an example of how to do this query.

Parse.com上也有此问题: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

This question is also on Parse.com: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

推荐答案

您可以在关系列上使用whereKey:equalTo:并将其传递给PFObject.然后,此查询将返回在其"students"关系中具有此student的所有Teacher对象:

You can use whereKey:equalTo: on a relation column and pass it a PFObject. This query will then return all Teacher objects which have this student in their "students" relation:

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:student];

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:student];

在此示例中,student是具有与"students"中的关系匹配的className的PFObject.如果这是PFUser的关系,并且您正在查找当前用户的"Teacher",请使用:

In this example, the student is a PFObject with a className that matches the relation in "students". If this is a relation of PFUsers and you're looking for the current user's "Teacher"s, you'd use:

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:[PFUser currentUser]];

PFQuery *query = [PFQuery queryWithClassName:@"Teacher"]; [query whereKey:@"students" equalTo:[PFUser currentUser]];

此答案还发布在Parse的社区论坛上: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

This answer also posted on Parse's community forums: https://parse.com/questions/how-do-i-query-using-pfrelation-when-i-just-have-pfuser

这篇关于解析:当我只有PFUser时,如何使用PFRelation查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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