2012-12-15 5 views
0

나는 다음과 같은 도메인이 있습니다부모에 대한 도메인의 저장 설정

class Attribute { 
    static hasMany = [attributeParameters: AttributeParameter] 
} 

class AttributeParameter { 

    String value 
    Integer sequenceNo 
    static belongsTo = [attribute: Attribute] 
} 

내가 저장을 자신의 값을 채우고 클릭하여 사용자 속성에 대한 기존의 모든 AttributeParameters를 표시하고 허용 할 양식이를 . 저장시 각 속성 매개 변수 (이미 ID가 있음)를 업데이트해야합니다.

현재이 작업을 수행하기 위해 HTML을 작성하는 방법에 대해 공백을 표시하고 있습니다.

코드가 명확하게 단순화 : :이 시도했습니다

<form> 
    <input type="hidden" name="attributeParameters[0].id" value="1" /> 
    <input type="text" name="attributeParameters[0].value" value="1234567" /> 
    <input type="hidden" name="attributeParameters[1].id" value="2" /> 
    <input type="text" name="attributeParameters[1].value" value="name" /> 
</form> 

def save() { 
    def attribute = Attribute.get(params.id) 
    attribute.properties = params 
} 

하고 올바르게 컬렉션을 채 웁니다하지만 AttributeParameter는 저장하기 전에 인출되지 않고 있기 때문에 그래서 그것은 작동하지 않습니다 오류로 인해 실패 :

:

나는 다음에 HTML을 수정 : UPDATE

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.foo.Attribute.attributeParameters

<form> 
    <input type="hidden" name="attributeParameters.id" value="1" /> 
    <input type="text" name="attributeParameters.value" value="1234567" /> 
    <input type="hidden" name="attributeParameters.id" value="2" /> 
    <input type="text" name="attributeParameters.value" value="name" /> 
</form> 

그리고 컨트롤러 :이 작동

params.list('attributeParameters').each { 
    def ap = AttributeParameter.get(it.id) 
    ap.value = it.value 
    ap.save() 
} 

. 내 유일한 관심사는 매개 변수가 들어오는 순서입니다. 매개 변수가 항상 params 객체에 들어 오면 양식에 나타나는 순서와 똑같은 순서로 나타나면 괜찮을 것입니다. 그러나 그들이 다르게 들어온다면 잘못된 AttributeParameter의 값을 수정할 수 있습니다.

그래도 여전히 매개 변수가 더 나은 방법이나 일종의 검증을 찾고 있다면 항상 먼저 선입견을 갖습니다.

UPDATE 2 :

나는 this post 가로 질러 그리고 내가 원하는 것입니다하지만 목록에 Attribute.attributeParameters을 변경할 수 없습니다. 그들은 세트로 있어야합니다.

답변

1

당신이 뭔가를 할 수 있습니다 :

각 AttributeParameter에서 동적으로 이름과 값을 만들
<form> 
    <input type="text" name="attributeParameter.1" value="1234567" /> 
    <input type="text" name="attributeParameter.2" value="name" /> 
</form> 

: 컨트롤러

params.attributeParameter.each {id, val-> 
    def ap = AttributeParameter.get(id) 
    ap.value = val 
    ap.save() 
} 

을에서 다음

<g:textField name="attributeParameter.${attributeParameterInstance.id}" value="${attributeParameterInstance.value}" /> 

그리고 그 당신은 각 매개 변수의 실제 id를 직접 가지며 어떤 순서 t 안녕하세요.

+0

네, 그게 좋은 일입니다. 고마워, 켈리. – Gregg