views:

31

answers:

2

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.

+1  A: 

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
Sven
Yes, I agree; it gave me a warning if I tried to make a parent relationship without an inverse.
zardon
How do you make to-many relationship, whenever I try it, it keeps doing a Many-to-Many relationship? Pic: http://img824.imageshack.us/img824/3780/parentchild.png
zardon
You need to add **two** relationships. You just set *parent_emp* to be it’s own inverse relationship. That doesn’t make sense for this. You also should really think about how you name your attributes and relationships. the usual way is to use CamelCase and not underscores to separate words.
Sven
Oh thanks, I appreciate your help. I just wasn't sure what I was meant to do with Core Data and how to make the relationships; but your image and detailed discussion helped alot. I have another query -- what is the best way to test that the parent-child relationship is working?
zardon
I've done some basic NSLogs similar to my answer below; and then I checked it with SQLite in the Terminal; it all seems to be working - I perhaps need to test it more, but I am happy none-the-less.
zardon
A: 

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.

zardon
Maybe a picture helps: http://yfrog.com/j8parentchilddonerightp
Sven
Thanks, that helped. I've been able to replicate your picture in xcode: http://img535.imageshack.us/img535/8409/parentchild2.png
zardon