CoreData - 如何使实体强制与其自身建立父子关系?

发布于 2024-09-25 09:02:28 字数 459 浏览 0 评论 0原文

我想创建一个名为“员工”的 CoreData 实体,一些“员工”可以有一个直线经理(或老板)。

在基本的伪代码中,它可以被描述为:

emp_id (PRIMARY KEY)
emp_name
emp_parent_id (INT *but optional as some people may not have line managers*)

我可以使用的另一个例子是“文章”,一篇文章可以有一个父“文章”,当然并非所有文章都有一个父文章。

我遇到的问题是,我不确定如何在核心数据中表示这一点,或者即使它可以处理此类事情。

在核心数据模型创建器中,我创建一个名为“Employee”的实体,然后使用可选的检查建立指向自身的关系,并确保不存在级联删除或不存在反向关系。

我不确定这是否是正确的方法。我可以使核心数据实体作为可选父实体与其自身相关吗?

谢谢。

I'm wanting to make a CoreData entity called "Employees", some "Employees" could have a line manager (or boss).

In basic pseducode, it could be described as:

emp_id (PRIMARY KEY)
emp_name
emp_parent_id (INT *but optional as some people may not have line managers*)

Another example I could use is "articles" an Article could have a parent "article", of course not all articles have a parent article.

The issue I'm having is, I'm not sure how to represent this in Core Data, or even if it can handle such things.

In the Core Data Model maker, I create an entity called "Employee" and then make a relationship which points to itself with optional checked and ensure that there is no cascading deletes, or no inverse relationship.

I am unsure if this is the right way to do it though. Can I make a core data entity relate to itself as an optional parent?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-10-02 09:02:28

当然可以。但您还应该添加逆关系,这样您就可以找出经理所管理的所有员工。

您的员工实体应该看起来有点像这样:

  • 关系经理:对一,可选,反向:管理员工
  • 关系管理员工:对多,可选,反向:经理

Sure you can. But you also should add the inverse relationship, so you can find out all the employees a manager is manager of.

Your employee entity should look somewhat like this:

  • Relationship manager: to-one, optional, inverse: managedEmployees
  • Relationship managedEmployees: to-many, optional, inverse: manager
数理化全能战士 2024-10-02 09:02:28

我已经能够使用 NSLog 制作一个基本版本。

NSManagedObjectContext *context = [self managedObjectContext];

Employee *boss = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" 
                           inManagedObjectContext:context];
boss.name = @"Mr Big";

Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" 
                          inManagedObjectContext:context];
emp.name = @"Mr Smith";
emp.parent_emp = boss;

NSError *error;
if (![context save:&error])
{
NSLog(@"Error -- %@", [error localizedDescription] );
}

// Now we loop through each entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *info in fetchedObjects) {
NSLog(@"Name: %@", [info valueForKey:@"name"]);
NSEntityDescription *parent = [info valueForKey:@"parent_emp"];
NSLog(@"Parent: %@", parent.name );
NSLog(@"------------");   
}

[fetchRequest release]

虽然我还在学习Core Data的基础知识。

@Sven - 我不能强制建立一对多关系,它总是给我多对多关系。所以目前我只是使用一对一的关系。

I've been able to do a basic version with NSLog.

NSManagedObjectContext *context = [self managedObjectContext];

Employee *boss = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" 
                           inManagedObjectContext:context];
boss.name = @"Mr Big";

Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" 
                          inManagedObjectContext:context];
emp.name = @"Mr Smith";
emp.parent_emp = boss;

NSError *error;
if (![context save:&error])
{
NSLog(@"Error -- %@", [error localizedDescription] );
}

// Now we loop through each entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *info in fetchedObjects) {
NSLog(@"Name: %@", [info valueForKey:@"name"]);
NSEntityDescription *parent = [info valueForKey:@"parent_emp"];
NSLog(@"Parent: %@", parent.name );
NSLog(@"------------");   
}

[fetchRequest release]

Although I am still learning the basics of Core Data.

@Sven - I cannot force the to-Many relationship, it always gives me many-to-many. So for the moment I am just using 1-to-1 relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文