Laravel 具有多个赋值的多态关系

发布于 2025-01-17 09:02:33 字数 847 浏览 3 评论 0原文

在我的Laravel项目中,我有这些模型:

User
Technique (belongs to User)
TechniqueAd (belongs to Technique)
ConstructionObject (belongs to User)
ConstructionObjectAd (belongs to ConstructionObject)
Cargo (belongs to User)
CargoAd (belongs to Cargo)

现在我需要创建另一个模型 - 交易,该模型拥有交易属性,

datetime (from)
datetime (to)
price
... etc.

但还必须拥有两个涉及交易的模型。例如,它可以是技术 + ConstructionObjectad或Object + Techniquead或Cargo + Techniquead等。实现此目的的最佳方法是什么?丑陋的方法是包括这些无效的字段:

technique_id
technique_ad_id
construction_object_id
construction_object_ad_id
cargo_id
cargo_ad_id

引用对相应模型的引用。每个数据库行总是只有2个ID值填充。但这似乎很丑陋...这是我应该使用多态关系的情况吗?例如,类似的内容是:

...
datetime (from)
datetime (to)
price
dealable_id
dealable_type
dealable_2_id
dealable_2_type

在这种情况下实现正确的系统结构的最佳方法是什么?

On my Laravel project I have these models:

User
Technique (belongs to User)
TechniqueAd (belongs to Technique)
ConstructionObject (belongs to User)
ConstructionObjectAd (belongs to ConstructionObject)
Cargo (belongs to User)
CargoAd (belongs to Cargo)

Now I need to create another model - Deal, which holds deal properties, for example:

datetime (from)
datetime (to)
price
... etc.

But also it has to hold two models that are involved in the deal. For example, it can be Technique + ConstructionObjectAd or Object + TechniqueAd or Cargo + TechniqueAd, etc. What is the best way to achieve this? The ugly way would be to include these nullable fields:

technique_id
technique_ad_id
construction_object_id
construction_object_ad_id
cargo_id
cargo_ad_id

That references to coresponding models. Each of the database rows would always have only 2 id values filled in. But that seems very ugly... Is this the situation where I should use polymorphic relationships? For example, something like:

...
datetime (from)
datetime (to)
price
dealable_id
dealable_type
dealable_2_id
dealable_2_type

What is the best way to achieve a correct system structure for this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文