我们有一个表名userProfile,它有一个指针userId to User 表。 我们在userProfile上应用内部查询
PFQuery *query = [PFQuery queryWithClassName:@"UserProfile"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
[query whereKey:@"relation" equalTo:@"Agent"];
[query includeKey:@"userId"];
[query includeKey:@"profileId.userId"];
query.skip = arrProfiles.count;
query.limit = 15;
NSString *searchText = [NSString stringWithFormat:@"%@",self.searchBar.text];
**PFQuery *innerQuery =[PFUser query];
[innerQuery whereKey:@"username" matchesRegex:[NSString stringWithFormat:@"(?i)%@",searchText]];
[query whereKey:@"userId" matchesQuery:innerQuery];**
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
}];
但在应用内部查询后我们无法获得任何结果。
答案 0 :(得分:0)
在查询中,您说userId必须等于当前用户。但是在内部查询中,您说userId必须将用户与在搜索字段中输入的用户名进行匹配。
这意味着查询必须满足BOTH约束才能返回结果。只有当前用户真正搜索自己时才会发生这种情况。
如果您正在寻找当前用户的个人资料和搜索到的个人资料,您需要使用或如下查询:
PFQuery *firstQuery = [PFQuery queryWithClassName:@"UserProfile"];
[query whereKey:@"userId" equalTo:[PFUser currentUser]];
NSString *searchText = [NSString stringWithFormat:@"%@",self.searchBar.text];
PFQuery *innerQuery =[PFUser query];
[innerQuery whereKey:@"username" matchesRegex:[NSString stringWithFormat:@"(?i)%@",searchText]];
PFQuery *secondQuery = [PFQuery queryWithClassName:@"UserProfile"];
[secondQuery whereKey:@"userId" matchesQuery:innerQuery];
PFQuery *query = [PFQuery orQueryWithSubqueries:[firstQuery, secondQuery]];
[query whereKey:@"relation" equalTo:@"Agent"];
[query includeKey:@"profileId.userId"];
query.skip = arrProfiles.count;
query.limit = 15;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
}];