2010-11-30 101 views
3

您好我有一个域,这么简单的像下面Grails将不保存,但没有错误

// page 52 Bankruptcy Questions 
class FamilyLawFinancial{ 

    Date dateOfTheOrder ; 
    boolean isPartyToFamilyLawProperty = false; 
    boolean isCurrentlyInvolvedInFamilyLawProperty = false; 
    boolean isBecomeInvolvedInProceedings = false; 

    static belongsTo=[person:Person]; 

    static constraints = { 
     dateOfTheOrder(nullable:true); 
     isPartyToFamilyLawProperty(nullable:false); 
     isCurrentlyInvolvedInFamilyLawProperty(nullable:false); 
     isBecomeInvolvedInProceedings(nullable:false); 
    } 

    String toString(){ 
     "${id}" 
    } 
} 

这里是保存数据的控制器:

def save = { 
    def person = Person.get(session.curperson.id); 
    def obj = FamilyLawFinancial.findByPerson(person); 
    if(obj == null){ 
     obj = new FamilyLawFinancial(); 
     obj.person = person ; 
    } 
    params.dateOfTheOrder = myutil.formatDate(params.dateOfTheOrder); 
    obj.properties = params; 

    println(obj.hasErrors()); 
    println(obj.dateOfTheOrder); 
    if(obj.hasErrors() && !obj.save(flush:true)){ 
     println("errors: ${obj.errors}"); 
     flash.message = "error found"; 
     println("save familyLawFinancial errors: ${errors}"); 
    }else{ 
     flash.message = "saved "; 
    } 
    redirect(controller:'frontPage', action:'index'); return ; 
} 

obj的。 hasErrors()产生false(这意味着没有错误),但它不会保存到数据库中。任何想法如何调试?

PS:myutil.formatDate() - >为日期字符串等转换为19/11/2010到日期()

回答

6
if(obj.hasErrors() && !obj.save(flush:true)){ 

&&后的条件将不被评估if计算前的状态到false

由于false && true评估为false,从语言的角度来看,评估第二个条件将是低效的。

毕竟,在这种情况下,obj.save(..)永远不会被调用。

+0

谢谢......我怎么没有意识到那件小事......> _ < – nightingale2k1 2010-11-30 08:18:23