在我的Web应用程序中,用户可以上传文档
或电子邮件
到频道
。
频道可以进一步拥有document_tags
和email_tags
,所有已上传的文档/电子邮件都应自动继承它们。
此外,document_tags
和email_tags
将拥有不同的描述:tag_descriptions
。例如,如果我们有一个文档,上传到具有标签animals(id = 1)
和pets(id = 2)
的频道:
文档#55
被上传到频道#8
。文档#55
将自动继承具有document_tags.channel_id = 55
的标签(可以使用以下关系访问:$channel->documenttags
)。 在这种情况下,是animals
和pets
。- 现在用户应该能够为
tag_descriptions
中的标签animals
和pets
设置唯一的描述,例如:
tag_descriptions
id | taggable_type | taggable_id | typeable_type | typeable_id | description
1 | App\DocumentTag | 1 | App\Document | 55 | My unique description for animals.
2 | App\DocumentTag | 2 | App\Document | 55 | My unique description for pets.
在上述数据库设计中,已上传的
文档 #55
具有标签:动物
和宠物
相关联,但进一步地,这两个标签有一个独特的描述,该描述对于特定的文档是唯一的。如果我上传另一个文档或电子邮件(假设
电子邮件 #20
),那么它看起来会像:
tag_descriptions
:id | taggable_type | taggable_id | typeable_type | typeable_id | description
1 | App\DocumentTag | 1 | App\Document | 55 | My unique description for animals.
2 | App\DocumentTag | 2 | App\Document | 55 | My unique description for pets.
3 | App\EmailTag | 1 | App\Email | 20 | Another unique description for animals.
4 | App\EmailTag | 2 | App\Email | 20 | Yet another unique description for pets.
现在,
电子邮件#20
也有标签animals
和pets
,但在这种情况下,用户可以为标签设置独特的描述。现在我的问题是:
以上设计可行,并且是否被视为 Laravel/PHP 的最佳实践?我有点不确定如何结构化代码,因为
TagDescription
模型将突然拥有两个多态关系(taggable
和 typeable
),而我在文档中找不到任何支持此功能的内容。此外,我不确定我能否使用上述设计通过特定上传的文档来访问唯一的描述,例如:
//In my Document.php model:
public function tagdescriptions()
{
return $this->morphMany(TagDescription::class, 'typeable');
}
然后像这样使用:$document->tagdescriptions
。
最后一点 - 我有点不确定如何保存特定标签的唯一标签描述,针对特定的可标记ID / 可标记类型和唯一电子邮件/文档。(typeable_id 和 typeable_type)。
document_tags
和email_tags
是否会随时间而改变,对于现有文档会有什么后果?这个问题应该同时回答新增标签和删除标签的情况。 - Namoshek