## 28 Working with data

## 29 Adding some data.

Here we have more complete example which writes a record to the database, then fetches the record and updates it and saves the data again, then removes the record.

    #include <Foundation/Foundation.h>
    #include <EOAccess/EOAccess.h>
    #include <EOControl/EOControl.h>

    int
    main(int arcg, char *argv[], char **envp)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      EOModelGroup *group = [EOModelGroup defaultGroup];
      EOModel *model;
      EOAdaptor *adaptor;
      EOAdaptorContext *context;
      EOAdaptorChannel *channel;
      EOEditingContext *ec;
      EODatabaseDataSource *authorsDS;
      NSArray *authors;
      id author;

      model = [group modelNamed:@"library"];

      /* Tools don't have resources so we have to add the model manually */
      if (!model)
        {
          NSString *path = @"./library.eomodel";
          model = [[EOModel alloc] initWithContentsOfFile: path];
          [group addModel:model];
          [model release];
        }

      adaptor = [EOAdaptor adaptorWithModel:model];
      context = [adaptor createAdaptorContext];
      channel = [context createAdaptorChannel];
      ec = [[EOEditingContext alloc] init];
      authorsDS
        = [[EODatabaseDataSource alloc] initWithEditingContext: ec
    				    entityName:@"authors"];

      [channel openChannel];

      /* Create a new author object */
      author = [authorsDS createObject];
      [author takeValue:@"Anonymous" forKey:@"name"];
      [authorsDS insertObject:author];
      [ec saveChanges];

      /* Fetch the newly inserted object from the database */
      authors = [authorsDS fetchObjects];
      NSLog(@"%@", authors);

      /* Update the authors name */
      [[authors objectAtIndex:0]
      	takeValue:@"John Doe" forKey:@"name"];
      [ec saveChanges];

      NSLog(@"%@", [authorsDS fetchObjects]);

      [channel closeChannel];
      [pool release];
      return 0;
    }

## 30 Working with relationships

Heres another more complex example of working with data, we’ll add an author, and some books, and then traverse the relationship in a couple of different ways.

    #include <Foundation/Foundation.h>
    #include <EOAccess/EOAccess.h>
    #include <EOControl/EOControl.h>

    int
    main(int arcg, char *argv[], char **envp)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      EOModelGroup *group = [EOModelGroup defaultGroup];
      EOModel *model;
      EOAdaptor *adaptor;
      EOAdaptorContext *context;
      EOAdaptorChannel *channel;
      EOEditingContext *ec;
      EODatabaseDataSource *authorsDS;
      EODataSource *booksDS;
      id author;
      id book;

      model = [group modelNamed:@"library"];

      /* Tools do not have resources so we add the model manually.  */
      if (!model)
        {
          NSString *path = @"./library.eomodel";
          model = [[EOModel alloc] initWithContentsOfFile: path];
          [group addModel:model];
          [model release];
        }

      adaptor = [EOAdaptor adaptorWithModel:model];
      context = [adaptor createAdaptorContext];
      channel = [context createAdaptorChannel];
      ec = [[EOEditingContext alloc] init];
      authorsDS
        = [[EODatabaseDataSource alloc] initWithEditingContext: ec
    				    entityName:@"authors"];

      [channel openChannel];

      author = [authorsDS createObject];
      [author takeValue:@"Richard Brautigan" forKey:@"name"];
      [authorsDS insertObject:author];

      booksDS = [authorsDS dataSourceQualifiedByKey:@"toBooks"];
      [booksDS qualifyWithRelationshipKey:@"toBooks" ofObject:author];

      book = [booksDS createObject];
      [book takeValue:@"The Hawkline Monster" forKey:@"title"];
      [booksDS insertObject:book];

      book = [booksDS createObject];
      [book takeValue:@"Trout Fishing in America" forKey:@"title"];
      [booksDS insertObject:book];

      [ec saveChanges];

      /* log the to many relationship from author to books */
      NSLog(@"%@ %@",
    	[author valueForKey:@"name"],
    	[author valueForKeyPath:@"toBooks.title"]);

      /* log the to one relationship from book to author */
      NSLog(@"%@", [book valueForKeyPath:@"toAuthor.name"]);

      /* traverse to one through the to many through key paths
         logging the author once for each book. */
      NSLog(@"%@", [author valueForKeyPath:@"toBooks.toAuthor.name"]);

      [channel closeChannel];
      [pool release];
      return 0;
    }