Default order in models

17 views
Skip to first unread message

sveri

unread,
May 6, 2012, 11:42:31 AM5/6/12
to circumfl...@googlegroups.com
Hi Boris,

this time i wonder if it is possible to supply a default order for models?
If i could do that i needn't set the .addOrder() criteria (very nice idea btw, these criterias) on every different fetch i am doing.

Best regards,
Sven

Boris Okunskiy

unread,
May 7, 2012, 6:16:41 AM5/7/12
to circumfl...@googlegroups.com
Greetings Sven,

No, there is no default order in Criteria. However, you can always stick with providing an utility method which instantiates criteria object with default settings:

```
object Foo extends Foo with Table[Long, Foo] {

val f = this AS "f"
def criteria = f.criteria.addOrder(r.createdAt ASC)

}
```

Best regards,
Boris Okunskiy

sveri

unread,
May 7, 2012, 1:56:33 PM5/7/12
to circumfl...@googlegroups.com
Hi Boris,

thanks, that looks nice, this way i can set default criterias for different behaviours :-)

I suspect using your criteria like that:
...
def fetchAllDone = criteria.add(Task.done EQ true).list
...
is the right way?

Best regards,
Sven

Boris Okunskiy

unread,
May 8, 2012, 3:51:20 AM5/8/12
to circumfl...@googlegroups.com
>
> I suspect using your criteria like that:
> ...
> def fetchAllDone = criteria.add(Task.done EQ true).list
> ...
> is the right way?

Certainly, as long as your `criteria` uses the default alias ("root"). If you have something like this:

```
val t = this AS "t"
def criteria = t.criteria.addOrder(t.createdAt ASC)
```

then you would have to refer to that `t` object instead of `Task`:

```
def fetchAllDone = criteria.add(t.done).list()
```

BTW, if `Task.done` resolves to BooleanField, then you can safely omit the `EQ true` part -- it should resolve to predicate implicitly.

Best regards,
Boris Okunskiy
Reply all
Reply to author
Forward
0 new messages