简体   繁体   English

解析-多对一查询

[英]Parse - Many to One Query

I have a query that gets latest posts by users. 我有一个查询,可以获取用户的最新帖子。 Each post has an "Owner" field that has the "ObjectId" of the account that created it. 每个帖子都有一个“所有者”字段,其中包含创建该帖子的帐户的“ ObjectId”。 I do this because I want to also display the username of the person who created the post - which is found in the User table. 我这样做是因为我还想显示创建帖子的人的用户名-在用户表中找到。

I looked at the Parse documentation for Queries and came up with this (modeled after one of their examples): 我查看了查询的Parse文档,并提出了这一点(以他们的示例之一为模型):

    var query = PFQuery(className:"Post")
    query.orderByDescending("createdAt");

    var innerQuery = PFQuery(className: "User")
    innerQuery.whereKey("objectId", matchesQuery: query)

Then I run the findObjectsInBackgroundWithBlock . 然后,我运行findObjectsInBackgroundWithBlock I don't think I'm getting the information from the User table to get the name of the person who posted the post. 我认为我不是从User表中获取信息来获取发布帖子的人的名字。

I looked at the object that contains the information for the post but I don't see the information for the user in that object. 我查看了包含帖子信息的对象,但是在该对象中看不到用户的信息。 What am I doing wrong? 我究竟做错了什么?

Here are screenshots of the headers of my two tables: 这是我的两个表的标题的屏幕截图:

POST: POST:

所有者应在下表中匹配ObjectId

USER: 用户:

这里的ObjectId应该与上一个表中的Owner相匹配

Thanks! 谢谢!

I think the problem as that you refer to the user table in the query as "PFQuery(className: "User")". 我认为问题在于您在查询中将用户表称为“ PFQuery(className:“ User”)“。 But if you use the built in user table, it's name is actually "_User". 但是,如果您使用内置的用户表,则其名称实际上是“ _User”。 So use that, or even simplier, use: 因此,使用它,或更简单地使用:

var innerQuery = PFUser.query()

Try this 尝试这个

    var class = PFQuery(className:"Post")
    var finduser:PFQuery = PFUser.query()
    finduser.whereKey("objectId", equalTo: class.objectForKey("owner").objectId)
    finduser.findObjectsInBackgroundWithBlock{
                (objects:[AnyObject]!, error:NSError!)->Void in
                if (error == nil){
                    if let userObjects = objects {
                        let UserResult = (userObjects as NSArray).lastObject as? PFUser
                        if let user = UserResult {
                           println(user.username)
                            }
                    }
               }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM