Laravel 具有多个赋值的多态关系
在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论