gpt4 book ai didi

mysql - EAV如何存储额外的字段值?

转载 作者:行者123 更新时间:2023-11-30 22:32:05 25 4
gpt4 key购买 nike

我在数据库和表中有一个 EAV 结构:attribute(id,parent_id,code,name)value(id,entity_id,attribute_id,value)

我正在使用 parent_id 来存储属性列表。例如,

id | parent_id | code | name
1 | null | color_id | Color
2 | 1 | null | Red
3 | 1 | null | Blue
4 | 1 | null | Other

现在我需要能够在选定的特定项目上存储不同的数据。这可能是一个或多个输入。例如:

Color:
(o) Red
(o) Blue
(o) Other (please specify) ______________

如何保存?创建额外的表或者可以存储 parent_id 指向选定的元素或其他东西?

id | parent_id | code | name
4 | 1 | null | Other
5 | 4 | other_one | First text value
6 | 4 | other_two | Second text value

最佳答案

稍微想了一下...这里试着给你一个“如何存储”这个问题的答案:

确保您的概念中包含部分结构。定义类型、值包、属于一起的事物并使用 XML。像上面这样的东西可能是这样的:

<settings>
<parent id="1">
<colors>
<color>Red</color> --here you should think of tags to set the meaning of the color...
<color>Red</color>
<color>Other</color> --here you set whatever color you want
</colors>
</parent>
<parent id="2">
-- Your example with the "First text value" gives me this idea:
<colors>
<color meaning="BackColor">Red</color>
<color meaning="BorderColor">Red</color>
<color meaning="First text value">Blue</color>
<color meaning="Second text value">Green</color>
</colors>
</parent>
</settings>

所以我的建议是:

尝试定义某些类型,如“FormColor”,让它像

<FormColor backcolor="Red" forecolor="blue" first_text="green">
--You may add specialities within the FormColor-element
<SpecialColor meaning="SomeSpecialMeaning">Blue</SpecialColor>
</FormColor>

将这些“类型”放在父元素中。您可以通过 id 轻松选择父元素并读取其内部 XML。在那里你可以找到你需要的一切......

通过这种方法,您可以像使用 EAV 一样灵活,但可读性、可维护性和评估性要好得多。

关于mysql - EAV如何存储额外的字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com