I am building a custom field feature for my application. This allows the administrator to add custom profile fields for users to fill in depending on the site’s needs.
The schema is simple
field_meta (store some metadata about the field)
================================================
id
type //type of field
field_name //name of the field in the fields table
render_data //Some data to use when rendering the form field
field
===================
id
name //Default field that can't be deleted
address //Default field that can't be deleted
customfield_1
customfield_2
The field_meta table is used to store some meta data for the field, so that we can render the form fields.
Each field is then stored in a new column in the field table.
Problem:
For usability and to not have to deal with users choosing reserved words or using non-english words for the column name, I will not be asking users to choose a name for the column name.
I am currently considering calling the column name by field type (there are quite a few types in the application (email, website, text, paragraphtext, etc), just to name a few) and adding a number. Some examples:
- Email_1
- Text_1
- Text_2
- Text_3
- Email_2
- etc
However, the problem with this approach is that it takes a fair bit of work to come up with the column name. I need to get all the columns, pick out the ones with the same column type as I am creating, parse for the largest number, and then create the column.
Is there a better strategy to do this? Or perhaps a totally different way to name the columns that eliminates these problems?
I decided to go with my original approach, that is to basically create a column name consisting of the type and an incremented number:
While it takes a fair bit of effort to generate the name, this is only done when creating the profile fields, so the performance hit would only occur during that time.