【发布时间】:2018-10-16 22:03:31
【问题描述】:
我想使用 Laravel Eloquent 多态关系,但它似乎不适用于我的表结构。
基本上我有一个游戏数据表,其中包含所有不同类型的游戏数据(国家、联赛、球队、球员等)。对于每种类型,我都有多个表格,其中包含由 game_id 分隔的信息。因此,gamedata 表中将有一行代表国家“England”,国家表中有 7 行对应的数据来自 7 个不同的 game_id。
我希望能够从 gamedata 表中选择一些行,并根据其类型从适当的表中选择它们对应的行。
这在一对一关系上很容易做到,但在一对多关系上似乎不可能。
这是游戏数据表。
CREATE TABLE `gamedata` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data_type` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `data_id` (`data_id`,`type`),
FULLTEXT KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
然后还有很多这样的表格(为了便于阅读,删除了很多列):
CREATE TABLE `nations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`game_id` tinyint(3) unsigned NOT NULL,
`gamedata_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`short_name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
/* more specific columns removed for ease of reading */
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `leagues` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`game_id` tinyint(3) unsigned NOT NULL,
`gamedata_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`short_name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
/* more specific columns removed for ease of reading */
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`game_id` tinyint(3) unsigned NOT NULL,
`gamedata_id` int(10) unsigned NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`short_name` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
/* more specific columns removed for ease of reading */
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
所以游戏数据表上的某些行可能如下所示:
(144, 'nation', 'Some Nation'),
(145, 'nation', 'Another Nation'),
(146, 'league', 'Some League'),
(147, 'league', 'Another League'),
(148, 'team', 'Some Team'),
(149, 'team', 'Another Team');
所以我应该能够从“data_type”列和“data_id”列做一个多态关系,从相应的表中获取对应的行。
但没有一个内置关系(morphTo、morphMany、morphedByMany)等似乎能够处理它。
似乎我想要的是morphTo() 关系,但它似乎将自己限制为仅返回一个相关模型。所有接受多个模型的关系都需要定义一个特定的模型。
// This would work fine if I only wanted one related model. "data_type" being the class and "id" corresponding to "gamedata_id" on relevent table.
$this->morphTo('data');
// These require me to be explicit about the class instantiating rather than using from the "data_type" column
$this->morphMany(???, 'data');
$this->morphToMany(???, 'data');
$this->morphedByMany(???, 'data');
有没有办法使用现有的 Laravel 关系来做到这一点?或者有没有一种简单的方法可以根据我的需要创建我自己的基于 morphTo 的关系类?
【问题讨论】:
-
乍一看,您的数据库结构看起来有点不寻常。
gamedata表的具体用途是什么?它的id是如何在其他地方引用的? -
游戏数据是一种简单的方法,可以为每个游戏数据类型提供标识符,例如国家“英格兰”。在国家数据中有 7 个“英格兰”行,每年一个。但是,如果您只想要一个来自游戏数据表的“英格兰”实体。同样,文章可以使用任何游戏数据类型进行标记,因此我有一个 article_gamedata 表,它将 article_ids 链接到 gamedata_ids。而不是 article_nations、article_leagues、article_teams 等
-
data_id引用id列时如何仍然有多个相关条目? -
抱歉,我在发布的内容中不小心遗漏了
gamedata_id列。就像我说的,如果 morphTo 只需要一列,它就可以正常工作,它会搜索gamedata_id并从gamedata_type找到合适的表。
标签: php laravel laravel-5 eloquent