与命名空间的多态雄辩关系 [英] Polymorphic Eloquent relationships with namespaces

查看:98
本文介绍了与命名空间的多态雄辩关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现多态关系。他们工作完美...但是,我试图尽可能减少我的数据库大小所以...我有这个

 表动作
| id | realatable_type | relatable_id
| 1 |狮子65
| 2 | Lion \Company | 13

显然我有这个

 <?php namespace Lion; 

class公司扩展\Eloquent {...}
类人员扩展\Eloquent {...}

有没有办法只存储人或公司,假定命名空间永远是狮子?

解决方案

由于Laravel 4.1,您的模型(在这种情况下公司)可以设置受保护的属性 $ morphClass 到任何你想要的。

 <?php命名空间狮子; 

class公司扩展\Eloquent {

protected $ morphClass ='Company';
}

现在在表中可以存储没有命名空间的类型:

  | id | realatable_type | relatable_id 
| 2 |公司| 13


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   |  Lion\People      |  65
|  2   |  Lion\Company     |  13

Obviously I've this

<?php namespace Lion;

class Company extends \Eloquent { ... }
class People extends \Eloquent { ... }

Is there any way to store only "People" or "Company" assuming that the namespace is always going to be "Lion"?

解决方案

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

这篇关于与命名空间的多态雄辩关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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