marcoarment / FCModel

An alternative to Core Data for people who like having direct SQL access.
MIT License
1.65k stars 173 forks source link

Adding default sorting option #30

Closed jasonsilberman closed 10 years ago

jasonsilberman commented 10 years ago

Is there a way you could do something like this?

[Person defaultOrder:@"ORDER BY pos"];

Then on queries such as allInstances it would automatically sort the array by that?

Maybe it could work by just appending that string?

Thanks!

P.S. I am totally loving this so far & am planning to use this in an upcoming app!

johncblandii commented 10 years ago

+1. I'd add it would be great if it did not negate the ability to add an ORDER BY per query through appending or querying without the order/replacing the default. That would add flexibility to allow for a default but specific queries elsewhere without the default.

[namely pulling from Rails' default_scope]

marcoarment commented 10 years ago

The concept of a default order-by would be somewhat inconsistent or vague, since it would only apply to certain queries and not others. (At least, without getting into the hacky business of trying to parse people's queries to determine whether they already contain a valid ORDER BY, which is a business I'd rather not get into.) I'd rather avoid it if possible.

You can already override allInstances in each model class, e.g. in the Person class:

+ (NSArray *)allInstances
{
    return [self instancesWhere:@"1 ORDER BY pos"];
}

Is this enough?

johnbland-wf commented 10 years ago

I can definitely understand the hesitation there. I guess you're right on just overriding.

jasonsilberman commented 10 years ago

That makes sense, thanks!