子查询和IN语句在领域 [英] Subquery and IN Statement in realm

查看:112
本文介绍了子查询和IN语句在领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个领域对象。部门,部门和用户。 section是某个部门的一个子部门。但是每个部门和每个部门都会有用户。

I have three realm objects. Department, section and user. section is a kind of sub department in a department. But there will be users under each section and each department.

@interface Department : RLMObject

@property NSString *name;
@property BOOL isCollapsed;

@property (nonatomic, strong) RLMArray<Section> *sections;
@property (nonatomic, strong) RLMArray<User> *users;

@end

@interface Section : RLMObject

@property NSString *name;
@property BOOL isCollapsed;
@property RLMArray<User> *users;

@end 

@interface User : RLMObject

@property NSString *department;
@property NSString *email;
@property NSString *firstname;
@property NSString *lastname;
@property NSString *fullname;

@end

我想要做的是,我想搜索所有名字和姓氏都包含a且与部门和部门相关联的用户。

What i want to do is, I want to search all the user whose first name and last name contains "a" and linked with department and section.

现在我正在做的是

searchText = [searchText lowercaseString];

RLMResults *sections = [Section objectsWhere:
                        [NSString stringWithFormat:@"SUBQUERY(users, $user, $user.fullname contains '%@' OR $user.nickname contains '%@').@count > 0",searchText,searchText]];

NSMutableArray *sectionNames = [NSMutableArray array];

for (Section *section in sections)
{
    [sectionNames addObject:section.name];
}

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(sections, $section, $section.name IN %@).@count > 0", sectionNames];

RLMResults *filteredDepartments = [[Department objectsWithPredicate:predicate] sortedResultsUsingProperty:@"name" ascending:YES];

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SUBQUERY(users, $user, $user.fullname contains %@ OR $user.nickname contains %@).@count > 0",searchText,searchText];
RLMResults *departments = [filteredDepartments objectsWithPredicate:pred];

目前问题是部分数组中有两个部分,但是 deparments 结果,它返回null。请帮忙。谢谢。

Right now the issue is there is two sections in sections array but in the deparments results, it returns null. please help. thanks.

注意:如果一个用户已经属于某个部分,那么该用户将不会被包含在部门中。

Note: If one user already belongs to section then that user will not be include in department.

推荐答案

也许你不需要使用子查询。更简单地说,您可以在查询中使用 ANY ,如下所示:

Maybe you do not need to use subquery. More simply, you can use ANY in query like the following:

[Department objectsWhere:
    @"ANY users.firstname CONTAINS %@ OR ANY users.lastname CONTAINS %@ OR ANY sections.users.firstname CONTAINS %@ OR ANY sections.users.lastname CONTAINS %@", searchText, searchText, searchText, searchText];

但我认为使用反向关系更容易。在Realm中,反向关系由 RLMLinkingObjects 定义。
您可以向用户类添加反向关系,如下所示:

But I think using inverse relationships is more easier. In Realm, inverse relationships is defined with RLMLinkingObjects. You can add inverse relationships to User class as follows:

@interface User : RLMObject

...

@property (readonly) RLMLinkingObjects *departments;
@property (readonly) RLMLinkingObjects *sections;

@end

@implementation User

+ (NSDictionary *)linkingObjectsProperties {
    return @{@"departments": [RLMPropertyDescriptor descriptorWithClass:Department.class propertyName:@"users"],
             @"sections": [RLMPropertyDescriptor descriptorWithClass:Section.class propertyName:@"users"]};
}

@end

然后你可以得到部门和用户所属的部分来自用户的属性,如下所示:

Then you can get departments and sections where the user belongs to from User's property, like the following:

RLMResults *users = [User objectsWhere:@"firstname CONTAINS %@ OR lastname CONTAINS %@" , searchText, searchText];
for (User *user in users) {
    NSLog(@"%@", user.departments);
    NSLog(@"%@", user.sections);
}

这篇关于子查询和IN语句在领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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