与命名空间的多态 Eloquent 关系 [英] Polymorphic Eloquent relationships with namespaces
本文介绍了与命名空间的多态 Eloquent 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试实现多态关系.他们完美地工作......但是,我试图尽可能地减少我的数据库大小,所以......我有这个
I've tried to implement polymorphic relationships. They work perfectly... However, I'm trying to reduce my database size as much as possible so... I've this
Table action
| id | realatable_type | relatable_id
| 1 | LionPeople | 65
| 2 | LionCompany | 13
显然我有这个
<?php namespace Lion;
class Company extends Eloquent { ... }
class People extends Eloquent { ... }
假设命名空间总是Lion",有没有办法只存储People"或Company"?
Is there any way to store only "People" or "Company" assuming that the namespace is always going to be "Lion"?
推荐答案
从 Laravel 4.1 开始,在你的模型中(在本例中为 Company 和 People),你可以设置受保护的属性 $morphClass
到任何你想要的.
Since Laravel 4.1, inside your model (in this case Company and People) you can set the protected property $morphClass
to whatever you want.
<?php namespace Lion;
class Company extends Eloquent {
protected $morphClass = 'Company';
}
现在你可以在你的表中存储没有命名空间的类型:
Now in your table you can store the type without the namespace:
| id | realatable_type | relatable_id
| 2 | Company | 13
这篇关于与命名空间的多态 Eloquent 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文