3

嗨,我有一个如此简单的域,如下所示

// 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 转换为 Date()

4

1 回答 1

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

&&如果之前的条件评估为 ,则不会评估之后的条件false

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

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

于 2010-11-30T06:00:43.873 回答