Laravel中的模型继承 [英] Model Inheritance in Laravel

查看:336
本文介绍了Laravel中的模型继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与Laravel合作,我正在努力,每个模型都需要从口才中延伸出来,我不知道如何实现一个模型层次结构(使用不同的表)



例如:



假设我有一个抽象模型工具,然后一个Model Hammer 和一个Model Screwdriver ,从工具中扩展。



现在,工具会延伸口才...但是,没有工具表,有一个锤子和另一个螺丝刀表,因为它们有不同的属性。



如何指定Hammer有一张桌子,当您的扩展工具时,螺丝刀有一张桌子?如何使用口述来调用,例如所有工具?



喜欢:

 工具:: all()

这应该带来所有的锤子和螺丝刀,因为他们都是工具



这是否可以使用Eloquent?

解决方案

虽然我喜欢多形态关系(PR)方法略好于单表继承( STI ),它仍然不觉得像真正的继承方法。从域(例如UML类图)的角度来看,它就像尝试使用组合关系而不是继承。



从数据库一致性的角度来看,多态关系本身已经是Laravel(IMHO)中非常奇怪的解决方案。由于没有单个字段是多个表的外键,因此可以导致加入不应该加入的ID。而没有遵守的反向关系。



尽管我并没有真正描述一个解决问题的方法,但我建议采用与PR和STI不同的方法。此解决方案将类似于 Hibernate的每个子类的表方法。我认为 Dauce扩展到雄辩正在朝着同一个方向发展,除了似乎还有一些实施问题仍然存在。 >

从数据库的角度来看,每个子类的表也意味着超类包含每个直接子类的一列。那么你可以在(非空)id上放置外键约束,并正确使用关系。然而,在Laravel的魔术表现中,还应该有一些额外的逻辑,将所请求的对象变成正确的类型。



所以对于我来说,功能上,雄辩的模特应该正确在PHP方面继承,而数据库仍然可以使用外键约束。


i'm currently working with Laravel and I'm struggling with the fact that every model needs to extend from Eloquent and I'm not sure how to implement a model Hierarchy (with different tables)

For Example:

Let's say I have an abstract model Tool, and then a Model Hammer and a Model Screwdriver that extend from Tool.

Now, Tool would extend Eloquent ... BUT, there is NO Table for Tools, there is a table for Hammers and another Table for Screwdrivers, because they have different attributes.

How do I specify that Hammer has a table and Screwdriver has a table when they both extend Tool? And how can I use Eloquent to call, For Example, All Tools?

Like:

Tools::all()

This Should bring all Hammers and Screwdrivers because they are all Tools

Is this possible using Eloquent?

解决方案

Even though I like the Polymorphic Relations (PR) approach slightly better than the Single Table Inheritance (STI), it still does not feel anything like a true inheritance approach. From the domain (e.g. a UML Class Diagram) point of view, it is like trying to use a composition relationship instead of inheritance.

From a database consistency point of view the polymorphic relation by itself is already a very weird solution in Laravel (IMHO). As there can be no single field that is a foreign key to multiple tables, this can lead to joining ID's that should not be joined. And inverse relationships that are not obeyed.

Although I'm not actually describing a solution to the problem, I'd propose a different approach than PR and STI. This solution would be similar to Hibernate's table-per-subclass approach. I think that Dauce's extension to Eloquent is going in the same direction, except there seem to be a few implementation issues still.

From a database point of view, a table per subclass would also mean that the super-class contains one column per direct subclass. Then you can put foreign key constraints on the (non-null) id's and properly use the relationships. However, there should still be some extra logic in Laravel's magic Eloquent class that turns the requested object into the right type.

So for me, functionally, the Eloquent models should properly inherit on the PHP side, while the database can still use the foreign key constraints.

这篇关于Laravel中的模型继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆