Menu

How to provide a default value for a date attribute

2014-06-10
2014-06-12
  • Arampatzis Aggelos

    I have successfully created a custom module to extent the UserRequest by adding a field of type AttributeDate. I now want to provide a default value (which needs to be a date) to this field.

    To start with, I want to provide the current date as the default value, but in the future I would like to be able to read the value from another entity (an SLA.)

    How can I achieve this?

    Thank you in advance for your time

     
  • Arampatzis Aggelos

    Trying to answer my own question I added the following in my extension's datamodel file:

    <default_value>2014-12-31</default_value>
    

    This works, but only in the administrator's UI and not in the user portal.

    Feedback anyone?

     

    Last edit: Arampatzis Aggelos 2014-06-11
  • Arampatzis Aggelos

    Another strange behavior I have noticed is this: if no default value is provided for the attribute I have added, when I am trying to create a new UserRequest from the administrator's UI, the attribute's UI widget displays by default a value like the following:

    Jun2014WedAMCESTEJune+0200RJunAMCEST

    In the user portal no such thing happens.

    NB: the attribute does not allow nulls.

     
    • Arampatzis Aggelos

      Looking through the schema for the UserRequest I just saw the following (for the attribute I added):

      Columns: date_due: DATE, Default: "Jun2014WedAMCESTEJune+0200RJunAMCEST", Null Allowed

      How is this possible? In the extension's module I have explicitly specified that nulls are now allowed:

      <field id="date_due" xsi:type="AttributeDate" _delta="define">
        <sql>date_due</sql>
          <default_value/>
        <is_null_allowed>false</is_null_allowed>
      </field>
      

      And where does iTop get the default value you see?

       
      • Arampatzis Aggelos

        By experimenting I found that this "weird" default value is present only when is_null_allowed is set to false.

        Which means that iTop is trying to provide a default value to obey the "null is NOT allowed" constraint.

        While on this subject, by checking the file attributedef.class.inc.php I found that for the AttributeDateTime class (parent of AttributeDate), method IsNullAllowed always returns true. Why is this the case?

         
        • Arampatzis Aggelos

          Ok this is getting stupid. I modifed the definition of method IsNullAllowed in class AttributeDateTime from this:

          public function IsNullAllowed() {return true;}
          

          to this:

          public function IsNullAllowed() {return $this->GetOptional("is_null_allowed", false);}
          

          and now when I go through the Data Model Toolkit I get all sorts of warnings like these:

          Class AsyncTask
          field 'created' in table 'priv_async_task' could NOT be NULL
          ALTER TABLE priv_async_task CHANGE created created DATETIME NOT NULL

          Class DBProperty
          field 'change_date' in table 'priv_db_properties' could NOT be NULL
          ALTER TABLE priv_db_properties CHANGE change_date change_date DATETIME NOT NULL

          Class CMDBChange
          field 'date' in table 'priv_change' could NOT be NULL
          ALTER TABLE priv_change CHANGE date date DATETIME NOT NULL

          Class Event
          field 'date' in table 'priv_event' could NOT be NULL
          ALTER TABLE priv_event CHANGE date date DATETIME NOT NULL

          The method's implementation has a comment about it, but I want to know if I can safely run the update script.

          Anyone?

           

          Last edit: Arampatzis Aggelos 2014-06-12

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.