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

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

问题描述

这是我正在尝试做的一个例子.我的当前用户是 PFUser,在另一个名为 Item 的类上,我有一个名为owners"的关系,它是 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 Teacher = ...//类Teacher"的 PFObject PFRelation studentRelation = [teacher relationforKey:@"students"];PFQuery *query = studentRelation.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 运算符需要有效指针.我的猜测是,因为我从一个空对象开始,它没有起点.带有教师的示例必须从填充的实例开始.我唯一填充的是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"];[查询 whereKey:@"students" equalTo:student];

在此示例中,student 是一个 PFObject,其 className 与 "students" 中的关系匹配.如果这是 PFUsers 的关系,并且您正在寻找当前用户的 "Teacher"s,则可以使用:

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"];[查询 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天全站免登陆