[英]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 目前,我要做的就是满足要求
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'];
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 帐户表*包含帐户等信息
Client table 客户表
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表//将包含可以定义/配置的表
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
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
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: 设计此类应用程序时面临的挑战是:
the PROS for this design is: 该设计的优点是:
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_id
, data_label
和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. 这样,您可以将每个帐户的额外数据保存到该表中,然后在account_info.php中进行获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.