[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: GDL2: fetching all objects from a database to a custom class
From: |
Manuel Guesdon |
Subject: |
Re: GDL2: fetching all objects from a database to a custom class |
Date: |
Fri, 22 Aug 2003 19:57:00 +0200 (CEST) |
Hi,
On Fri, 22 Aug 2003 10:31:34 +0200 Frédéric PRACA
<frederic.praca@freebsd-fr.org> wrote:
>| Hi everybody,
>| today I tried to make a sample (and simple) application using GDL2.
>| The goal of this application is to connect to a database and retrieve data
>to
>| put it in custom classes. The question will be simple : how do I do that ? I
>| searched in headers files but even if I can find method names, it's
>impossible
>| to find the sequence to use.
>| Here's what I've done :
>| - construct a model from a file
>| - create a EODatabase from that model
>| - create an EOQualifier with format
>| - create an EOFetchSpecification with entity
>| - retrieve an EOEditingContext from EODatabase
>| - applied the context the objectsWithFetchSpecifiation and the NSArray is
>| empty ;-(
>| In fact, I just wanted to retrieve all the objects of my custom class from
>the
>| database. Is there a straight way to do that ? Where can I find code
>examples
>| and an howto-like ?
>|
Here is a exemple to fetch a record, change one of it's property and save it:
// Create Model Group
EOModelGroup *group = [EOModelGroup new];
// The path to eomodeld directory
NSString* modelPath=@"/Path/To/My.eomodeld";
// Adapt Postgres date format
[NSCalendarDate setPostgres95Format:@"%m/%d/%Y %H:%M:%S%z"];
// add a model based on our eomodeld directory
dbModel=[group addModelWithFile:modelPath];
// Be paranoiac.
NSAssert(dbModel,@"No dbModel");
[EOModelGroup setDefaultGroup:group];
[group release];
// Create database
EODatabase* database = [[[EODatabase alloc] initWithModel:_dbModel]
autorelease];
// Create database context with database
EODatabaseContext* dbContext = [[[EODatabaseContext alloc]
initWithDatabase:database] autorelease];
// Yes, we want debugging information
[[dbContext adaptorContext] setDebugEnabled:YES];
// add objectStore
[[EOObjectStoreCoordinator defaultCoordinator]
addCooperatingObjectStore:dbContext];
// Create Editing Context
EOEditingContext* myEditingContext=[EOEditingContext new];
// Get the record which have 1 as code from entity 'Test'
EOGenericRecord* aRec=[myEditingContext objectMatchingValue:@"1"
forKey:@"code"
entityNamed:@"Test"];
// Dump the record
NSLog(@"aRec=%@",aRec);
NSLog(@"Property XX of the record=%@",[aRec valueForKey:@"XX"];
// Now change XX property
[aRec takeValue:@"blabla"
forKey:@"XX"];
// Verify the change :-)
NSMLog(@"aRec=%@",aRec);
// Save change
[myEditingContext saveChanges];
Instead of fetching one record matching a value, you can use other methods
defined in EOUtilities.h
Have fun !
Manuel
--
______________________________________________________________________
Manuel Guesdon - ORANGE CONCEPT <mguesdon@orange-concept.com>
14 rue Jean-Baptiste Clement - 93200 Saint-Denis - France
Tel: +33 1 4940 0997 - Fax: +33 1 4940 0998