2016-09-24 61 views
0

使用Grails域對象,我偶然發現了一個試圖設置某些屬性的問題。Grails在對象引用上設置元類屬性

var stepchild=parent.children.find{ it.id==xInt }; 
stepchild.metaClass.birthMom=biologicalMothersName; 

parent.children.each{child-> 
    //when it gets to stepchild no metaclass property birthMom exists.... 
} 

而且自定義錯誤沒有得到引用的對象

var stepchild=parent.children.find{ it.id==xInt }; 
stepchild.errors.rejectValue('parent',"Not biological parent"); 

parent.children.each{child-> 
    //when it gets to stepchild no errors.... 
} 

我猜找{}不會返回一個真正的參考父母的孩子子對象上設置?

回答

0

對於您要設置冷落對象上的屬性/屬性的方式,你應該使用MetaClass.setAttribute()方法:

stepchild.metaClass.setAttribute(stepChild,'birthMom', biologicalMothersName) 
+0

確定。任何想法,但錯誤屬性? – user2782001

+0

不完全確定,你沒有提供很多關於你的域對象的代碼。父母是stepChild域對象的實際屬性? – pczeus