Search code examples
mysqlgrails

Grails - Date 0000:00:00 00:00:00


I need to be able to put, in a bad way, my variable "lastupdate" 0000: 00: 00 00:00:00, but I do not know how to do it since being a date gives me an error

class Dialernumbers {
    Integer campaign
    Date lastupdate

    static mapping = {
                    id column: "id", type:"long", sqlType: "int", generator: 'increment'
                        version false
                    }

       static constraints = {
                    lastupdate nulleable: true
                    }

}

Solution

  • Issabel - Asterisk sounds like a co worker :) anyhow what I suggest is you don't do any of above instead do this create another map inside the class

    class Dialernumbers {
        Integer campaign
        Date lastupdate
    
        static mapping = {
                        id column: "id", type:"long", sqlType: "int", generator: 'increment'
                            version false
                        }
    
    
           Map loadValues() { 
               Map m =[:]
               m.campaign=this.campaign
               m.lastUpdate=this.lastUpdate ? this.lastUpdate : '00/00/00'
    
               return m
           }
           static constraints = {
              lastupdate nullable: true
           }
    
    }
    

    Now when you want to call class for Issabel - Asterisk you call

    Dialernumbers dl = Dialernumbers.get(1L)
    Issabel.dateValue = dl.loadValues().lastUpdate 
    
    //Map values  = dl.loadValues()
    //Issabel.dateValue = values.lastUpdate  
    

    This way you keep something that is a date field as a date field so it is then available for Hibernate and db queries just like a date and is also nullable in which case is returned in a specific format for your current requirements using loadValues and could be altered to another method for something else nullable NOT nulleable