简体   繁体   English

Laravel Eloquent - 一张表的多个模型

[英]Laravel Eloquent - Multiple models for one table

I have 3 eloquent models which are pretty much te same and represent discounts:我有 3 个雄辩的模型,它们几乎相同并且代表折扣:

  • AbsoluteDiscount绝对折扣
  • OneProductForFreeDiscount一个产品免费折扣
  • QuantityDiscount数量折扣

They all will get a method called "canBeApplied(Productable $productable)".他们都将获得一个名为“canBeApplied(Productable $productable)”的方法。 Which does have a look at the given productables and then determine if the productable will be eligible for the discount it represents.哪个确实会查看给定的产品,然后确定产品是否有资格获得它所代表的折扣。 The implementation of this method is different for those 3 different Discounts.对于这 3 种不同的折扣,此方法的实现是不同的。

Now, their attributes are pretty much the same.现在,它们的属性几乎相同。 They all have these attributes: name, active, valid_from, valid_trough.它们都具有这些属性:name、active、valid_from、valid_trough。 So i think i would need to have one single database table with these fields / column to represent those 3 discount eloquent models:所以我想我需要有一个包含这些字段/列的单一数据库表来表示这 3 个折扣雄辩模型:

  • id ID
  • name姓名
  • discountable_type discountable_type
  • active积极的
  • valid_from有效来源
  • valid_until有效期至

The column discountable_type will then the the fully qualified class names of the discounts. discountable_type 列将成为折扣的完全限定类名称。 In case of a 10 and 20 dollar gift card you would use the name "Gift card - 10 dollar" and "20 dollar" and both of them the discountable_type of "Namspace\For\AbsoluteDiscount\AbsoluteDiscount".如果是 10 美元和 20 美元的礼品卡,您将使用名称“礼品卡 - 10 美元”和“20 美元”,它们都是“Namspace\For\AbsoluteDiscount\AbsoluteDiscount”的 discountable_type。

What i am thinking i am needing to do is:我在想我需要做的是:

  • Create a base eloquent model for those discount models to extend.为那些要扩展的折扣模型创建一个基本的雄辩模型。
  • Override the where method in each discount, appending an additional where method call to it with this as its content ('discountable_type', '=', AbsoluteDiscount::class) before returning it.覆盖每个折扣中的 where 方法,在返回之前附加一个附加的 where 方法调用,并将其作为其内容 ('discountable_type', '=', AbsoluteDiscount::class)。

But then we also have methods: get, first, all and maybe more that need to be overridden.但是我们也有方法:get、first、all 甚至更多需要重写的方法。 And what about saving?那么储蓄呢? And these steps need to be done for all 3 discount models.所有 3 个折扣模型都需要完成这些步骤。 Is there an easy way to do it like this or is this something i must not do?有没有一种简单的方法可以这样做,或者这是我不能做的事情?

I think you can use this trait here: https://github.com/Nanigans/single-table-inheritance我想你可以在这里使用这个特性: https ://github.com/Nanigans/single-table-inheritance

However, it based on singleTableTypeField attribute, which doesn't allow to get a child by conditions, only by some kind of type_id column.但是,它基于 singleTableTypeField 属性,不允许按条件获取子项,只能按某种类型的 type_id 列。 But I think it would be not so difficult to modify it to fit your needs.但我认为修改它以满足您的需要并不难。

Well, this isn't a Polymorphic relation but looks a bit like it. 好吧,这不是多态关系,但看起来有点像它。 A polymorphic relation specifies that a model belongs to one or more other models. 多态关系指定一个模型属于一个或多个其他模型。 This is different situation since these models don't belong to other models but are all alike. 这是不同的情况,因为这些模型不属于其他模型,但都相似。 I don't know what a solution for this could be 我不知道对此有什么解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM