在文本字段中存储表引用(适用于 Agile Toolkit)
我最近一直在尝试 RDB 设计,我想知道如何将项目存储在可以具有多个值的字段中:
CARS Color_avail
1 corvette 1, 2, 3 <<<<<<<
2 ferrari 2
3 civic 1
COLORS
1 red
2 White
3 black
因此在 CRUD 上,我想通过下拉菜单/复选框或可以保存的内容添加多个项目多个值。
I have been experimenting with RDB design lately, and I was wondering about storing items in a field that can have more than one value:
CARS Color_avail
1 corvette 1, 2, 3 <<<<<<<
2 ferrari 2
3 civic 1
COLORS
1 red
2 White
3 black
so on CRUD I would like to add more than one item via a drop down / checkboxes or something that would hold multiple values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以看到在表单中显示这样的输出的好处,但是您真的想将其像这样存储在数据库中吗?
例如,对于包含逗号分隔列表的数据模型(如您的示例中所示),您将使用什么 SQL 来识别所有可用的白色汽车?
保持像这样的多对多关系的传统方法是使用附加表,例如您有一个单独的表,其中包含具有以下内容的 CAR_COLOUR
所以现在您可以轻松查询诸如获取所有汽车和颜色的列表
或如果如果您只想要白色汽车,请将以下内容添加到 WHERE 子句中
,在 id 字段和 CAR_COLOUR 中的两个字段上添加索引,这意味着即使您有数千行,也能获得出色的性能,而将它们全部放在逗号分隔的列表中单场将意味着你必须使用 substr 或类似的东西,这会阻止使用索引,并且意味着随着数据量的增长,性能会迅速下降。
I can see the benefit of displaying the output like this in a form, but do you really want to store it like this in the database ?
For example with a datamodel that holds a comma separated list as in your example, what SQL would you use to identify all the cars available in white ?
The traditional way to hold a many to many relationship like this is to use an additional table e.g. you have a separate table that holds CAR_COLOUR with the following contents
So now you can easily query things like, get a list of all cars and colours
OR if you just want the white cars, add the following to the WHERE clause
an index on the id fields and on both fields in CAR_COLOUR will mean you get great performance even if you have thousands of rows whereas putting them all in a comma separated list in a single field will mean you have to use substr or like which would prevent the use of indexes and mean as the amount of data grows, the performance will degrade rapidly.
从某种意义上说,将关系存储在逗号分隔的列表中是有意义的。不过你不需要逗号。有 2 个现有控件可以帮助您实现这一点。
使用表单中的复选框显示值列表:(
您可以通过 $model->getRows() 填充数组,尽管我认为它需要关联。您可以使用 var_dump 和 foreach 将它们连接起来) 。
您的其他选择是使用带有可选网格的隐藏字段。
要隐藏实际字段,您可以使用“hidden”而不是“line”或使用 JavaScript 来隐藏它:
或者
如果您也需要隐藏字段周围的标记。
Storing relations in the coma-separated list makes sense in some senses. You don't need commas though. There are 2 existing controls which can help you with that.
Displaying list of values with checkboxes in a form:
(you can populate array through $model->getRows() although I think it needs to be associative. You can probably join them with var_dump and foreach).
Your other options is to use a hidden field with selectable grid.
To hide the actual field, you can either use "hidden" instead of "line" or use JavaScript to hide it:
or
if you need to hide markup around the field too.