AlexDenisov / iActiveRecord

ActiveRecord for iOS without CoreData, only SQLite
http://alexdenisov.github.com/iActiveRecord/
MIT License
354 stars 50 forks source link

make name format of foreign key columns definable #57

Closed jklimke closed 10 years ago

jklimke commented 10 years ago

Currently it seems to me that foreign keys for the database are always assumed to be in CamelCase. it would be nice if this could be configured somehow.

E.g. if you have a Contact-Model and an Address model like the following, the foreign key must (???) be like referencedModeNameId and cannot be called referenced_model_name_id.

@interface Contact : ActiveRecord

@property NSString* phone;
@property NSString* email;
@property NSString* name;
@property NSNumber* addressId;

belongs_to_dec(Address, address, ARDependentDestroy);

@end

@implementation Contact

@dynamic name, phone, email, addressId;

belongs_to_imp(Address, address, ARDependencyDestroy)

@end 

@interface Address : ActiveRecord

@property NSString* streetLine1;
@property NSString* streetLine2;
@property NSString* zip;
@property NSString* city;
@property NSDecimalNumber* lat;
@property NSDecimalNumber* lon;

@end

@implementation Address

@dynamic streetLine1, streetLine2, zip, lat, lon, city;
@end

If this could be changed (optionally), you could easily use a small Rails application to e.g. build a content database to feed you application. That means i would like to have a foreign key like ´´´address_id´´´to be generated

Would this be easily possible ?

AlexDenisov commented 10 years ago

Yes, I think this could be done without problems. We already have similar stuff here

jklimke commented 10 years ago

hi,

i tried to customize the relation foreign key columns by adding selectors named after the original columns. This works very well except for adding objects to belongs_to relations. this is because the setRecord method use the setValue methods directly and do not use selectors as it is done with has_many_through relations. Is there a reason for this ?

Otherwise i could implement something like this and it would work well for me:

´´´ @implementation NBContact

@dynamic name, phone, email, address_id;

belongs_to_imp(NBAddress, address, ARDependencyDestroy)

has_many_through_imp(NBTopic, NBContactGroup, topics, ARDependencyNullify)

This results in the following (desired) table scheme:

contacts id name phone email address_id created_at updated_at

jklimke commented 10 years ago

I also noticed, that if the table name for models ist defined through overwriting + (NSString*) recordName in the model class, it is not used consistently throught the framework. Especially when using joins or has_many / has_many_through relation, the class name is used for select statements.

AlexDenisov commented 10 years ago

@jklimke, nice catch. Seems it's bug. I'll fix it, PR are welcome too.

jklimke commented 10 years ago

OK. I will see what i can do.

jklimke commented 10 years ago

This should be fixed with the pull request https://github.com/AlexDenisov/iActiveRecord/pull/59 (did accidentially open up a new issue)