简体   繁体   中英

How to create dynamic field for a database using PHP

I have a multi-client level PHP application that I wrote. This application has many database object just like any other PHP app. But since it is a multi-client level app, I have different client requirements every time we on board a new client. There are couple common object (aka accounts) that host most of the client information. But blmost, every time I want to add new client to the database, this new client is going to require a new column in the accounts databases added because they will have some unique data. There are standard required data (ie. account_name, status......), but some data are unique and labeled different based on the client business type.

Currently, what I have to do to accommodate the requirements is

  1. Alter the accounts table and add this new column manually.
  2. edit the PHP script where I display the account information and display the content of the field like so

    if(!empty($row['new_filed'])) echo 'Single Client Required Field: ' . $row['new_filed'];

  3. if the field is editable, then I have to add rules to the edit/add form to do a data validation check before save.

I am trying to build a front end tool that will does that automatically for me. This way I can have an admin "not a programer" go the the front end tool and add a new field and that field will be displayed in the selected section of the selected page.

From the front end tool I want to be able to say add a custom field called "brand_c" and make it of a type varchar(100). Then on the page that is called "account_info.php" in the section called "Other" display this field with the label "Brand" only if the client = 'XYZ' so if the data belong to client 'ABC' do not display the data since this column is custom only for 'XYZ'.

I understand this is a wide question but what I am looking for is any help of how such a thing can be build (were to start from?). This is something that most of the big CRM do "ie. Microsoft CRM Dynamic, Salesforce.com ...) have it. How can I architect something like this?

Thank you

The first step you will need to do is design the tables properly, so basically you need to know how to design table to support dynamic fields and the field will be able to validate/handle such such data.

What I did with my application(dynamic application where the UI fields can be configured in the application's admin) is saving the configuration in the database, each time there is a transaction, the page will fetch the configuration for the selected biller and it will generate the required fields. and when the form is submitted, the application will again fetch the configuration and make validation based on the setting in client_field

Account table *Contain information such as account

  • account_id //primary key of this table
  • account_name
  • account_status

Client table

  • client_id //primary key of this table
  • account_id //foreign key:account table
  • client_name
  • client_status

you can add different column for the client depending on your requirement, but make sure not to add the required UI fields in this table, instead create the table like below

client_field table //will contain table that can be defined/configured

  • field_id //primary key of this table
  • client_id //foreign key:client table
  • field_name //the field name that will appear in the user interface
  • field_title //appear when on mouseover on the field
  • field_datatype //this can be text,integer,date,boolean,etc
  • field_type //field input type: this can be a textbox,datepicker,dropdown list,etc
  • field_required //flag to indicate whether the field is required
  • field_display //flag to indicate whether the field will appear/hidden in the user interface
  • field_defaultvalue //default value to set when the page renders
  • field_minvalue //minimum value the field will accept: only acceptable when dataytpe is integer/double
  • field_maxvalue //same as field_minvalue description instead it will check the maximum integer/double value
  • field_maxlength //max length for the field
  • field_order //order of the field in the interface
  • field status //activate or deactivate the field

You can add additional column but you need to make sure it will be fluid and not redundant.

let us say, you have field_type dropdown list, you need to create another table, lets call it

field_dropdown

  • id //primary key for this table
  • field_id //foreign key from client_field
  • dropdown_value //the value that will be used in the dropdown list when selected
  • dropdown_description //the description that will appear in the dropdown list
  • dropdown_order //order of the item
  • dropdown_status //flag whether this record is active/inactive

You might be wondering how to save the data if this is the case, the data must be saved in another table, lets call it

client_information

  • id //primary key for this table:
  • client_id //foreign key from the client table
  • field_id //foreign key from client_table.
  • value //the value from the user interface

this way, when you save a new client, each field in the client_field will add a new row in the client_information table along with the value inserted by the user

the challenges in designing this kind of application are:

  • it is quite hard to develop (lots of condition and mapping, and require alot of time)
  • you will need to properly configure the fields in order for it to properly work
  • proper system design,architecture, and flow otherwise it will ruin the dynamicity of the system
  • you will need proper documentation so that you will understand which is which.
  • the process will be linear for all the clients except if you can alter your own flow by saving them into the database

the PROS for this design is:

  • once it is finished, you do not require anymore developer to add additional code except for software enhancement such as feature that is not supported when it was released.
  • the application will be dynamic and adding any field will be easy and maintainable via the admin module

I am not saying this is the MUST DO, but this is my suggestion on how to develop application that requires dynamic data fields, and so far from my experience in developing one, it did work well just as long it is well design and developed.

Instead of adding additional fields as a new column into your accounts table, you could create a new table, say extra_data , with for example the columns account_id , data_label , and data_content . This way you could save as many extra pieces of data per account into that table, and fetch it in account_info.php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM