如何通过解析中的多对多连接表检索相关对象

时间:2015-12-17 22:04:15

标签: swift parse-platform

我有3个表,User,Book和Vote。用户可以向上或向下投票。我正在尝试检索用户尚未投票的所有图书。

我试图使用

var voteQuery = PFQuery(className:"Vote")
voteQuery.whereKey("user", equalTo: PFUser.currentUser()!)

var bookQuery = PFQuery(className:"Book")
bookQuery.whereKey("books", doesNotMatchKey:"book", inQuery:voteQuery)

bookQuery.findObjectsInBackgroundWithBlock {
    (books: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        // The find succeeded.
        print("Successfully retrieved \(books!.count) books.")
        // Do something with the found objects
        if let books = books {
            print("books")
            print(books)
            for book in books {
                print(book.objectId)
            }
        }
    } else {
        // Log details of the failure
        print("Error: \(error!) \(error!.userInfo)")
    }
}

这将返回所有书籍,而不会遗漏用户投票的书籍。相反,如果我使用

,我得不到任何结果
bookQuery.whereKey("books", matchesKey:"book", inQuery:voteQuery)

非常感谢任何帮助!谢谢。

用户:

{ "results": [
    {
        "bcryptPassword": "xx",
        "createdAt": "2015-12-17T20:15:42.023Z",
        "email": "email@example.com",
        "objectId": "9uoWak96sp",
        "phone": "415-392-0202",
        "sessionToken": "xx",
        "updatedAt": "2015-12-17T20:15:42.023Z",
        "username": "myUsername"
    }
] }

图书:

{ "results": [
    {
        "createdAt": "2015-12-17T20:15:42.510Z",
        "objectId": "3rKWI2qI0b",
        "title": "Book B",
        "updatedAt": "2015-12-17T20:15:42.510Z"
    },
    {
        "createdAt": "2015-12-17T20:15:42.383Z",
        "objectId": "zsEhE9QCMU",
        "title": "Book A",
        "updatedAt": "2015-12-17T20:15:42.383Z"
    }
] }

投票:

{ "results": [
    {
        "book": {
            "__type": "Pointer",
            "className": "Book",
            "objectId": "zsEhE9QCMU"
        },
        "createdAt": "2015-12-17T20:15:42.647Z",
        "objectId": "gfIRXbPtyf",
        "upVote": true,
        "updatedAt": "2015-12-17T20:15:42.647Z",
        "user": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "9uoWak96sp"
        }
    }
] }

0 个答案:

没有答案