在文本字段中存储表引用(适用于 Agile Toolkit)

发布于 2024-12-04 10:01:54 字数 299 浏览 1 评论 0原文

我最近一直在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌若浮生 2024-12-11 10:01:54

我可以看到在表单中显示这样的输出的好处,但是您真的想将其像这样存储在数据库中吗?

例如,对于包含逗号分隔列表的数据模型(如您的示例中所示),您将使用什么 SQL 来识别所有可用的白色汽车?

保持像这样的多对多关系的传统方法是使用附加表,例如您有一个单独的表,其中包含具有以下内容的 CAR_COLOUR

 CAR      COLOUR
 1        1
 1        2
 1        3
 2        2
 3        1

所以现在您可以轻松查询诸如获取所有汽车和颜色的列表

SELECT CAR, COLOUR
  FROM CARS CA, 
       COLOUR COL, 
       CAR_COLOUR CACOL
 WHERE CA.CAR=CACOL.CAR
   AND CACOL.COL=COL.COLOUR

或如果如果您只想要白色汽车,请将以下内容添加到 WHERE 子句中

   AND COL.COLOUR='White'

,在 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

 CAR      COLOUR
 1        1
 1        2
 1        3
 2        2
 3        1

So now you can easily query things like, get a list of all cars and colours

SELECT CAR, COLOUR
  FROM CARS CA, 
       COLOUR COL, 
       CAR_COLOUR CACOL
 WHERE CA.CAR=CACOL.CAR
   AND CACOL.COL=COL.COLOUR

OR if you just want the white cars, add the following to the WHERE clause

   AND COL.COLOUR='White'

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.

如梦初醒的夏天 2024-12-11 10:01:54

从某种意义上说,将关系存储在逗号分隔的列表中是有意义的。不过你不需要逗号。有 2 个现有控件可以帮助您实现这一点。

使用表单中的复选框显示值列表:(

$form->addField('CheckboxList','corvette')->setValueList($array);

您可以通过 $model->getRows() 填充数组,尽管我认为它需要关联。您可以使用 var_dump 和 foreach 将它们连接起来) 。

您的其他选择是使用带有可选网格的隐藏字段

$field = $form->addField('line','selection');
$grid = $form->add('MVCGrid');
$grid->setModel('Colors',array('name'));
$grid->addSelectable($field);

$form->addSubmit();

要隐藏实际字段,您可以使用“hidden”而不是“line”或使用 JavaScript 来隐藏它:

$field->js(true)->hide();

或者

$field->js(true)->closest('dl')->hide();

如果您也需要隐藏字段周围的标记。

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:

$form->addField('CheckboxList','corvette')->setValueList($array);

(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.

$field = $form->addField('line','selection');
$grid = $form->add('MVCGrid');
$grid->setModel('Colors',array('name'));
$grid->addSelectable($field);

$form->addSubmit();

To hide the actual field, you can either use "hidden" instead of "line" or use JavaScript to hide it:

$field->js(true)->hide();

or

$field->js(true)->closest('dl')->hide();

if you need to hide markup around the field too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文