The actual scope examples provide the use of hard-coded parameters passed to the query:
public class Employee extends Model {
static {
addScope("byDepartment", "department = 'marketing'");
}
}
Is it possible to make that parameter dynamic and all the scope as follows:
public class Employee extends Model {
static {
addScope("byDepartment", "department = '?'");
}
}
Example of use:
Employee.scope("byDepartment").where(....) <-- how to pass in a department value ?
Thank you.
Current implementation only works with hard-coded scopes. In general, having dynamic scopes is not any different than just having an additional parameter in a
where()
method, but will significantly complicate the implementation.This question prompts for some philosophical discussion. Normally, you would be using a model as its own service. In other words, using a model like this from outside the model is not a preferred way:
It is best to wrap all access to the
EMPLOYEES
table into theEmployee
class like this:We code all JavaLite projects with this pattern, and do not allow ActiveJDBC API to bleed outside models (for the most part, lol).
As you can see, there is little that scopes will give you as the internal implementation of the model may or may not use scopes, you will get the same results. This coding pattern is much better becuase:
if (department = null) throw new IllegalArgumentException("blah...")
However, if you use this approach, the value of scopes is near zero.
Needless to say, I do not use scopes in my work.