简体   繁体   English

如何使用PHP为数据库创建动态字段

[英]How to create dynamic field for a database using PHP

I have a multi-client level PHP application that I wrote. 我有一个编写的多客户端级别的PHP应用程序。 This application has many database object just like any other PHP app. 像任何其他PHP应用程序一样,此应用程序具有许多数据库对象。 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. 有标准的必填数据(即account_name,status ...),但是某些数据是唯一的,并且根据客户业务类型而被标记为不同。

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 编辑我在其中显示帐户信息并显示字段内容的PHP脚本,如下所示

    if(!empty($row['new_filed'])) echo 'Single Client Required Field: ' . if(!empty($ row ['new_filed']))echo'单个客户端必填字段:'。 $row['new_filed']; $行[ '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). 在前端工具中,我想说的是添加一个名为“ brand_c”的自定义字段,并使其类型为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'. 然后,仅在客户端=“ XYZ”的情况下,在“其他”部分中名为“ account_info.php”的页面上才显示带有标签“品牌”的字段,因此,如果数据属于客户端“ ABC”,则不会显示数据,因为此列仅针对“ 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? 这是大多数大型CRM所做的事情(即Microsoft CRM Dynamic,Salesforce.com ...)。我该如何构建类似的东西?

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. 我对应用程序(可在应用程序的admin中配置UI字段的动态应用程序)所做的工作是将配置保存在数据库中,每次发生事务时,页面将获取所选记帐器的配置,并将生成必填字段。 and when the form is submitted, the application will again fetch the configuration and make validation based on the setting in client_field 提交表单后,应用程序将再次获取配置并根据client_field中的设置进行验证

Account table *Contain information such as account 帐户表*包含帐户等信息

  • account_id //primary key of this table account_id //此表的主键
  • account_name 用户名
  • account_status 帐户状态

Client table 客户表

  • client_id //primary key of this table client_id //此表的主键
  • account_id //foreign key:account table account_id //外键:账户表
  • client_name 客户名称
  • client_status 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 您可以根据需要为客户端添加不同的列,但请确保不要在此表中添加必需的UI字段,而是创建如下表

client_field table //will contain table that can be defined/configured client_field表//将包含可以定义/配置的表

  • field_id //primary key of this table field_id //此表的主键
  • client_id //foreign key:client table client_id //外键:客户表
  • field_name //the field name that will appear in the user interface field_name //将出现在用户界面中的字段名称
  • field_title //appear when on mouseover on the field field_title //在鼠标悬停在字段上时显示
  • field_datatype //this can be text,integer,date,boolean,etc field_datatype //可以是文本,整数,日期,布尔值等
  • field_type //field input type: this can be a textbox,datepicker,dropdown list,etc field_type //字段输入类型:可以是文本框,日期选择器,下拉列表等
  • field_required //flag to indicate whether the field is required field_required //标志,指示是否需要该字段
  • field_display //flag to indicate whether the field will appear/hidden in the user interface field_display //指示该字段是否将在用户界面中显示/隐藏的标志
  • field_defaultvalue //default value to set when the page renders field_defaultvalue //页面渲染时设置的默认值
  • field_minvalue //minimum value the field will accept: only acceptable when dataytpe is integer/double field_minvalue //该字段将接受的最小值:仅当dataytpe为整数/双精度时可接受
  • field_maxvalue //same as field_minvalue description instead it will check the maximum integer/double value field_maxvalue //与field_minvalue描述相同,但它将检查最大整数/双精度值
  • field_maxlength //max length for the field field_maxlength //字段的最大长度
  • field_order //order of the field in the interface field_order //界面中字段的顺序
  • 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_type下拉列表,需要创建另一个表,让我们称之为

field_dropdown field_dropdown

  • id //primary key for this table id //此表的主键
  • field_id //foreign key from client_field field_id // client_field中的外键
  • dropdown_value //the value that will be used in the dropdown list when selected dropdown_value //选择时将在下拉列表中使用的值
  • dropdown_description //the description that will appear in the dropdown list dropdown_description //将显示在下拉列表中的描述
  • dropdown_order //order of the item dropdown_order //商品的顺序
  • dropdown_status //flag whether this record is active/inactive dropdown_status //标记此记录是否处于活动状态

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 client_information

  • id //primary key for this table: id //此表的主键:
  • client_id //foreign key from the client table client_id //来自客户表的外键
  • field_id //foreign key from client_table. field_id // client_table中的外键。
  • value //the value from the user interface value //来自用户界面的值

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 这样,当您保存新客户端时,client_field中的每个字段都会在client_information表中添加新行以及用户插入的值

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 . 您可以创建一个新表(例如extra_data ,而不是将其他字段作为新列添加到accounts表中,例如,使用account_iddata_labeldata_content列。 This way you could save as many extra pieces of data per account into that table, and fetch it in account_info.php. 这样,您可以将每个帐户的额外数据保存到该表中,然后在account_info.php中进行获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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