문제

Gorm을 사용하여 사용자 객체를 정의했다고 가정 해 봅시다. 각 사용자는 0 이상으로 로그인 할 수 있습니다. 각 로그인에는 타임 스탬프가 있습니다. user.logins를 검색 할 때 Login.Date의 값에 따라 로그인을 정렬하기를 원합니다. 이것을 달성하기위한 올바른 성배 방법은 무엇입니까?

예 : 다음 코드가 모든 사용자의 로그인을 오름차순 순서로 나열하려고합니다.

<g:each var="login" in="${user.logins}">
  <tr>
    <td>${login.date}</td>
  </tr>
</g:each>

이들은 참조 된 클래스입니다.

class User {
  ...
  def hasMany = [logins: Login]
  static fetchMode = [logins: "eager"]
}

class Login {
  Date date
  ...
  def belongsTo = [User]
}

나는 최신 안정적인 릴리스 인 Grails 1.0.4를 실행하고 있습니다.

도움이 되었습니까?

해결책 3

Grails/Gorm에서 기본 정렬 순서 처리는 Grails 1.1에서 근본적으로 단순화 된 것으로 보입니다.

다른 팁

참조 안내서 (섹션 5)의 Gorm 페이지 에서이 작업을 수행하는 방법을 보여줍니다. 당신이 원하는 비트는 해당 문서의 맨 아래 근처에 있습니다. 두 가지 간단한 예가 있습니다.

class Airport {
    …
    static mapping = {
        sort "name"
    }
}

class Airport {
    …
    static mapping = {
        sort name:"desc"
    }
}

또한 협회에서 정렬의 예가 있습니다.

class Airport {
    …
    static hasMany = [flights:Flight]
    static mapping = {
        flights sort:'number'
    }
}

로그인 클래스가 비슷한 인터페이스를 구현하게하십시오.

class Login implements Comparable {

    // ...

    Date date

    public int compareTo(def other) {
        return date <=> other?.date // <=> is the compareTo operator in groovy
    }

}

그리고 관계를 정렬 세트로 선언합니다.

class User {
  ...
  def hasMany = [logins: Login]               
  SortedSet logins

  static fetchMode = [logins: "eager"]
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top