FEATURE REQUEST field default values not currently set at DB level

5 posts by 3 authors in: Forums > CMS Builder
Last Post: May 19, 2014   (RSS)

By kitsguru - May 16, 2014

I just realized that the default values for fields are not set at the database table level.

When a record is added via a form outside the Admin, the default values should be honoured automatically. This would be the case if the default value for the column was set at table create/alter.

Jeff Shields

By gregThomas - May 16, 2014

Hi kitsguru,

I've passed this request on to our senior developer as something to look into adding in the future.

For now the best way to ensure that blank values are added to columns that you don't have data for is to use the mysql_insert function that comes with CMS Builder. Here is a example of how to use it:

  $colsToValues = array(
    'title'   => 'Escape',
    'content' => '<p>An escape is a car made by Renault.</p>',
    'brand'   => 'Renault'
  );
  mysql_insert('cars', $colsToValues, true);
  echo 'done';

Although this method won't insert the default values, it will enter empty content into the field, and no errors will be thrown if there isn't a default value for the field.

The first value in mysql_insert is the table name, the second is an array of columns and values (these will be automatically validated to be mysql safe), the final option is if you want blank values to be used if no value was provided for a field.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By kitsguru - May 16, 2014

hi Greg, 

I did use the mysql_insert function, then verified my data and checked the database itself to see that it was not behaving as I expected.

I already added the proper default values to my insert, but the whole point of a default value is not to have to do this at all. It should be done via the create table or alter table commands as appropriate.

Jeff

Jeff Shields

By kitsguru - May 19, 2014

Understand the edge cases can't be done, but any others could and should be.

So if it is an edge case do what you do now, if a static value then set the default at the database level. OR maybe a checkbox for us to control what happens with static values.

MYSQL Default Rules:

If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.

If the column cannot take NULL as the value, MySQL defines the column with an explicit DEFAULT clause, using the implicit default value for the column data type. Implicit defaults are defined as follows:

  • For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence.

  • For date and time types other than TIMESTAMP, the default is the appropriate zero value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See Section 11.3, “Date and Time Types”.

  • For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.

BLOB and TEXT columns cannot be assigned a default value.

Jeff Shields