Laravel的多态关系是否也适用于真正的多态性? [英] Are Laravel's Polymorphic Relations also suitable for true polymorphism?

查看:98
本文介绍了Laravel的多态关系是否也适用于真正的多态性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地解释自己:Laravel.com上的示例展示属于视频和帖子评论 的关系.但是,我说的是规范:不属于父表,而是成为规范. ( Subtyping )使用Laravel的Polymorphic Relations还是最好的方法吗? /p>

一个非常基本(并且可能很糟糕)的示例.

解决方案

有趣的问题.

利用Laravel提供的多态关系,完全可以满足您的要求.但是,您指的是表继承,这就是鱼的全部.

您可以让您的Animal模型具有可以是FishMamal的多态关系,或者您可以让FishMamal属于Animal,并创建传递-通过模型.

这确实取决于您将如何使用它.您会像查询animals表一样浏览Animal,还是要从孩子那里走?

To explain myself better: the examples on Laravel.com showcase a relation of comments belonging to both videos and posts. However I'm talking about specification: not belonging to, but being a specification of the parent table. (Subtyping) Is using Laravel's Polymorphic Relations still the best approach?

A very basic (and potentially bad) example.

解决方案

Interesting question.

It is entirely possible to do what you require, with the polymorphic relation that Laravel provides. You however, are referring to table inheritance which is whole other kettle of fish.

You could have your Animal model have a polymorphic relationship that can either be Fish or Mamal, or you could have Fish and Mamal belong to an Animal, and create a pass-through model.

It really depends exactly on how you'll be using this. Will you be going through Animal as in querying the animals table, or will you be going from the children?

这篇关于Laravel的多态关系是否也适用于真正的多态性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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