We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    I'm having a bit of trouble setting a default value for a "date" field in an object's map. I would like the default value to be the current date when creating a new item. How would I go about doing that in the schema.xml?
            <field key="valid_from" dbtype="date" phptype="date" null="false" default="???"/>
    

      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 1778
      • 659 Posts
      Hi susan

      Didi you try to set 'attributes' instead of default ?

      <field key="valid_from" dbtype="date" phptype="date" null="false" attributes="ON CREATE CURRENT_TIMESTAMP"/>
      


      Like the "ON UPDATE CURRENT_TIMESTAMP" attribute (which act on update) it should do what you want...

      Cheers
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Reading the MySQL documentation, it turns out that it's not possible (without creating a custom trigger) to use a function such as date() as a default value except for a timestamp, which is not really appropriate in this case.

        I'm next turning attention to the ExtJS create new function, so that's probably where I'll need to insert the default date.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 1778
          • 659 Posts
          Ooops
          Too fast reading ^_^ I didn't notice you had set a type 'date' for your field... I generally use timestamps to store dates in the db, it's easy then to return the date (or the part of) in the format I need ...
          Cheers
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            I'll need a valid_from and a valid_to date, so timestamps aren't appropriate. I'm learning how to configure the ExtJS grids, so that's where I'll initially set up the default value. Then when the new item is saved (or for whatever reason an existing item loses its date), it'll have a value to save.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              <field key="valid_from" dbtype="date" phptype="date" null="false" default="CURRENT_TIMESTAMP" />

              I think there's a MySQL limitation allowing only one of those per table (object) though.
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.