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 Dave - May 19, 2014

Hi Jeff, 

Here's some background on where we're at with that feature request, it's come up a few times:  

I really like the idea of having the default fields set automatically at the DB layer, but some of the default values have some edge cases where that won't work such as when they are calculated (+5 days for a date field) or allow <?php ?> tags in the "Default Value" field, or are used for user instructions that are intended to be replaced on data entry: "Enter PO# Here".

We've considered building that functionality into our mysql_* convenience functions, but I'm a bit on the fence about that as I like them to be independent of cmsb structures, 

Another approach we could have is to have a function that returned all the defaults for a particular schema.  eg: $fieldsToValues = getSchemaDefaults($schemaName); or something like that.

We're in the middle of re-writing the field library to work like the plugins systems (and potentially be extensible allowing users to add field types as easily as they add plugins), so hopefully that will make it easier as right now the default values are all being calculated in /lib/menus/default/edit_functions.php as each field is displayed.

In any case, I'll put it on the feature request list and we'll can revisit it once we get back on the field library (or if you need something sooner you could hack together a getSchemaDefaults type function manually).

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com