[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Design issue for MVC and database updates



Well the problem is not in the database methods really but more what I 
must implement to see which object's how has changed. But I supply  the 
method anyway:


- (IBAction)saveToDb:(id)sender {
	
	NSEnumerator *iterator;
	JDTransactionType *item;
	int newId;
	
	iterator = [objectArray objectEnumerator];
	while(item = [iterator nextObject]) {
		if ([item rowId] > 0) {
			[connection updateQuery:
				[NSString stringWithFormat:@"UPDATE transactiontypes SET code='%@', 
name='%@',  positioneffect=%i, cashEffect=%i WHERE rowid=%i",
					[item code], [item name], [item positionEffect], [item 
cashEffect], [item rowId]]];
		} else {
			newId = [connection insertQuery:
				[NSString stringWithFormat:@"INSERT INTO transactiontypes(code, 
name, positionEffect, cashEffect) VALUES('%@', '%@', %i, %i)",
				[item code], [item name], [item positionEffect], [item 
cashEffect]]];
			if (newId != 0) {
				[item setRowId: newId];
			}
		}
	}
}

Some comments to the code:
I use the sMySQL framework so connection is a MCPConnection. 
objectArray is the array that is bound to NSArrayController and rowId 
is the primary key for the database table which I set to -1 in the 
model object's init mehod.

Joakim


On 2004-03-02, at 23.16, Scott Stevenson wrote:

>
> On Mar 2, 2004, at 11:16 AM, Joakim Danielson wrote:
>
>> I have a function that stores the rows in my array (bound to 
>> NSArrayController) to the database with insert or update commands. 
>> The problem is that all old objects get updated in the database even 
>> if the have not been update in the GUI. So I need some kind of dirty 
>> flag to keep track of objects that have really changed.
>
> Can you post the methods where you do the database updates?
>
>     - Scott
>
> --
> Tree House Ideas
> http://treehouseideas.com/
>
> _______________________________________________
> MacOSX-dev mailing list
> MacOSX-dev@xxxxxxxxxxxxx
> http://www.omnigroup.com/mailman/listinfo/macosx-dev
>