-
Notifications
You must be signed in to change notification settings - Fork 38
Sample Model
ziminji edited this page Nov 28, 2013
·
16 revisions
Writing an Object Relational Mapping (ORM) model is easy. Below is a sample ORM model, which you can use to write your your own.
This sample ORM model is for the following SQLite table:
CREATE TABLE "Person" ("pk" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "firstName" VARCHAR(35), "lastName" VARCHAR(35), "isActive" BOOL DEFAULT 1);
Here is the model's header file for the 'Person' table:
//
// Person.h
// MyApplication
//
// Created by Ziminji on 04/05/12.
// Copyright 2012 Ziminji. All rights reserved.
//
#import "ZIMOrmModel.h"
/*!
@class Person
@discussion This class represents an SQLite database table.
@updated 2012-03-08
*/
@interface Person : ZIMOrmModel {
@protected
NSNumber *_pk;
NSString *_firstName;
NSString *_lastName;
NSNumber *_isActive;
}
@property (strong, nonatomic) NSNumber *pk;
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) NSNumber *isActive;
@end
The model's logic file for the 'Person' table is as follows:
//
// Person.m
// MyApplication
//
// Created by Ziminji on 04/05/12.
// Copyright 2012 Ziminji. All rights reserved.
//
#import "Person.h"
@implementation Person
@synthesize pk = _pk;
@synthesize firstName = _firstName;
@synthesize lastName = _lastName;
@synthesize isActive = _isActive;
- (instancetype) init {
if ((self = [super init])) {
_saved = nil;
}
return self;
}
+ (NSString *) dataSource {
return @"live";
}
+ (NSString *) table {
return @"Person";
}
+ (NSArray *) primaryKey {
return [NSArray arrayWithObject: @"pk"];
}
+ (BOOL) isAutoIncremented {
return YES;
}
@end
To simplify this process, you can also use the BASH script in the "gen" folder to generate your models from an existing database. See, the tutorial on Generating Models.