2010-11-30 72 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

相關問題