English 中文(简体)
为自定义字段动态地将新列添加到表格
原标题:Adding new columns to table dynamically for custom fields
  • 时间:2012-05-23 04:23:05
  •  标签:
  • mysql

我正在为我的应用程序建立一个自定义字段功能。 这使管理员能够添加用户根据网站需要填写的自定义配置字段 。

计划很简单

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

field_meta 表格用于存储字段的某些元数据,以便我们可以转换窗体字段。

然后将每个字段储存在 field 表格中的新列中。

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.

目前,我正在考虑按字段类型(应用程序中有相当几种类型(电子邮件、网站、文本、段落等)命名列名,仅举几个例子)并添加一些数字。

  • Email_1
  • Text_1
  • Text_2
  • Text_3
  • Email_2
  • etc

然而,这个方法的问题在于,要得出列名需要相当多的工作。 我需要把所有的列名都取出来, 选出与我所创建的列型相同的列, 以最大数量分析, 然后创建列 。

是否有更好的战略可以做到这一点? 或者用完全不同的方式命名消除这些问题的栏目?

最佳回答

我决定采用我原来的方法,即基本上建立一个由类型和递增号组成的专栏名称:

  • Email_1
  • Text_1
  • Text_2
  • Text_3

虽然生成此名称需要相当努力, 但只有在创建配置文件字段时才这样做, 所以性能撞击只有在那个时候才会发生。

问题回答

不要试图动态地添加列, 这是一种缓慢( 而非好形式) 的方法 。

相反, 请在新表格中存储额外的列, 每个列都有自己的 ID 。 然后在另一个表格中存储用户 ID、 额外的列 ID 和该用户的属性信息 。 这将仿照添加新列而不必这样做 。

Edit: Indeed it does sound like EAV. Schema:
Attributes{id:int, name:string}
Users{id:int, name:string, (any others needed…)}
Users_attributes{Attribute_id:int, User_id:int, Attribute_info:string}

然后将任何新的属性输入属性( 加上递增 ID) 和任何新的用户信息, 并输入到用户和属性身份的用户和属性属性属性中 。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签