gpt4 book ai didi

php - 数据库设计 : menus table for a cms

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

我想为 cms 设计 menus 表。我已经有了 categoriescontents 表。菜单是指向特定类别或内容的链接,也可以是来自其他网站的 URL。

如何定义每个菜单是一个类别还是一个内容?我教过最好有 category_idcontent_idurl 列,它们都可以为 null。

这是一个好方法吗?什么是最好的方法?

我是否应该添加一个名为 menu_type 的列来定义菜单类型,它可以具有以下值:url、category 或 content。是否需要像这样创建另一个表:

编号 |类型名称

1 |类别

2 |内容

3 |网址

并在 menu_type 列中为内容的类别 2 插入 1...作为外键?

非常感谢。

最佳答案

根据您的要求,您可以构建M-M Polymorphic Relationships 类别内容菜单之间。

你的表结构应该是这样的:

categories
id - integer
name - string
...

contents
id - integer
name - string
...

menus
id - integer
body - text
owner_id - integer
owner_type - string
...

你的模型应该是这样的:

class Menu extends Model
{
/**
* Get all of the owning owner models.
*/
public function owner() // <--------- Get the owner model object
{
return $this->morphTo();
}
}

class Category extends Model
{
/**
* Get all of the post's owner.
*/
public function menus() // <--------- Get all the associated Menu Model objects
{
return $this->morphMany('App\Menu', 'owner');
}
}

class Content extends Model
{
/**
* Get all of the video's owner.
*/
public function menus() // <--------- Get all the associated Menu Model objects
{
return $this->morphMany('App\Menu', 'owner');
}
}

url 的情况下,您可以让 owner_idowner_type 字段为空,或者您可以定义一个额外的列 type 来定义菜单的类型。

希望这对您有所帮助!

关于php - 数据库设计 : menus table for a cms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41246560/

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