Php 数据库设计:cms的菜单表
我想为cms设计Php 数据库设计:cms的菜单表,php,mysql,laravel,database-design,Php,Mysql,Laravel,Database Design,我想为cms设计菜单表。我已经有了类别,和内容表格。菜单是指向特定类别或内容的链接,也可以是来自其他网站的url 如何将每个菜单定义为类别或内容? 我教过有category\u id、content\u id和url列都可以为空是很好的 这是一个好方法吗?最好的办法是什么 我应该添加一个名为menu\u type的列来定义菜单的类型,它可以有以下值:url、category或content。是否有必要创建另一个这样的表: class Menu extends Model { /**
菜单
表。我已经有了类别
,和内容
表格。菜单是指向特定类别或内容的链接,也可以是来自其他网站的url
如何将每个菜单定义为类别或内容?
我教过有category\u id
、content\u id
和url
列都可以为空是很好的
这是一个好方法吗?最好的办法是什么
我应该添加一个名为menu\u type
的列来定义菜单的类型,它可以有以下值:url、category或content。是否有必要创建另一个这样的表:
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');
}
}
id|键入|u名称
1 |类
2 |内容
3 | url
在“菜单类型”列中,为内容的类别2插入1。。。作为外键
非常感谢。根据您的要求,您可以在
类别
、内容
和菜单
之间建立一个链接
您的表结构应如下所示:
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');
}
}
类菜单扩展模型
{
/**
*获取所有拥有者模型。
*/
公共函数所有者()//morphTo();
}
}
类类别扩展模型
{
/**
*获取所有该帖子的所有者。
*/
公共函数菜单()//morphMany('App\Menu','owner');
}
}
类内容扩展模型
{
/**
*找到视频的所有所有者。
*/
公共函数菜单()//morphMany('App\Menu','owner');
}
}
在url
的情况下,您可以让owner\u id
和owner\u type
字段为空,或者您可以定义一个额外的列type
来定义菜单的类型
希望这有帮助 根据您的需求,您可以在
类别
、内容
和菜单
之间建立一个链接
您的表结构应如下所示:
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');
}
}
类菜单扩展模型
{
/**
*获取所有拥有者模型。
*/
公共函数所有者()//morphTo();
}
}
类类别扩展模型
{
/**
*获取所有该帖子的所有者。
*/
公共函数菜单()//morphMany('App\Menu','owner');
}
}
类内容扩展模型
{
/**
*找到视频的所有所有者。
*/
公共函数菜单()//morphMany('App\Menu','owner');
}
}
在url
的情况下,您可以让owner\u id
和owner\u type
字段为空,或者您可以定义一个额外的列type
来定义菜单的类型
希望这有帮助 谢谢!所以我想存储实际的url字符串,如
http://stackoverflow.com
在一列中,所以我必须有一个链接
列用于此?是的,您也可以在菜单表中有该列!谢谢所以我想存储实际的url字符串,如http://stackoverflow.com
在一列中,所以我必须有一个链接
列用于此?是的,您也可以在菜单表中有该列!如果没有任何特定于该类型的额外属性,可以使用enum()
列。使用它,字符串“category”、“content”和“url”只存储一次,每个记录上存储一个ID。但是,当您查询表时,MySQL知道返回字符串而不是ID。您也可以通过字符串或ID进行搜索。因此WHERE type=0
和WHERE type='category'
将返回大量相同的内容。这是一个很好的解决方案!如果没有任何特定于该类型的额外属性,可以使用enum()
列。使用它,字符串“category”、“content”和“url”只存储一次,每个记录上存储一个ID。但是,当您查询表时,MySQL知道返回字符串而不是ID。您也可以通过字符串或ID进行搜索。因此WHERE type=0
和WHERE type='category'
将返回大量相同的内容。这是一个很好的解决方案!