2016-09-24 62 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