LoginSignup
2
2

More than 5 years have passed since last update.

Slim3でリレーションシップを使ったモデルをクエリで検索してみる

Posted at

リレーションしたモデルのModelRefに対してフィルターかけたい場合の対応方法。流石にリレーション先のプロパティでの合致は無理そうだけど、Keyで一致できるだけでもかなり便利そう。

例:部署(1)対従業員(多)の場合

model
// 部署
@Model(schemaVersion = 1)
public class Dept implements Serializable {
}

// 従業員
@Model(schemaVersion = 1)
public class Employee implements Serializable {
    private ModelRef<dept> deptRef = new ModelRef<dept>(Dept.class);

    public ModelRef<dept> getDeptRef() {
        return deptRef;
    }
}
service
/**
 * ある部署の全従業員を取得する
 */
public List<employee> getEmployeeList(Key deptKey) {
    EmployeeMeta employeeMeta = new EmployeeMeta();
    ModelRefAttributeMeta<employee, ModelRef<Dept>, Dept> refMeta = 
        employeeMeta.deptRef;

    List<employee> list = 
        Datastore.query(Employee.class).filter(refMeta.equal(deptKey));

    return list;
}

参考
【備忘録】Slim3のModelRefでクエリ
http://blog.tyato.jp/2011/07/slim3modelref.html

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2