Laravel雄辩的模型-模型扩展模型 [英] Laravel eloquent model - model extends model

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

问题描述

是否有可能用另一个模型扩展laravel 4雄辩模型,比如说我有一个扩展雄辩类的用户模型,另外还有一个第二类,即扩展了用户类的管理员类?

is it possible to extend a laravel 4 eloquent model with another model, let's say i have a user model extending the eloquent class and additionally there is a second class, a administrator class extending the user class?

如果我只是将管理员类链接到用户,则必须先获取用户的管理员属性,然后获取admins属性,才能访问管理员属性.

If i just linked the administrator class to the user i'd have to access the administrators attributes by first getting the administrator attribute of the user and then getting the admins attributes.

比方说,我让管理员不扩展用户.我必须访问例如像这样的电话号码(管理员属性) $user = User::find(1); $phone = $user->administrator->phone; 但是通过让管理员扩展用户,我可以像这样直接访问电话号码 $user = Administrator::find(1);(请注意,为找到管理员而传递的ID与用于获取用户的ID相同.通常,我必须在Administrator数据库中传递条目的真实ID) $phone = $user->phone; 同时,可以访问用户类别的属性,例如$phone = $user->email;

Let's say I have the Administrator not extending the User. I'd have to access e.g. the phone number(a administrators attribute) like this $user = User::find(1); $phone = $user->administrator->phone; but by letting the Administrator extend the User I am able to access the phone number directly like this maybe $user = Administrator::find(1); (Note that the id passed to find the Administrator is the same like the one I use to get the user. Normally I would have to pass the real id of the entry in the Administrator database) $phone = $user->phone; At the same time it would be possible to access an attribute of the user class e.g. $phone = $user->email;

或者也许有更好的解决方案来实现此目标,或者像这样使用它毫无意义,如果是这样,请随时告诉我

or maybe there is a better solution to achieve this or it makes no sense to use it like this, if so, feel free to tell me

推荐答案

从原则上讲,这是一个好主意,而在实践中则是个坏主意.如果两个模型都使用同一张表,并且唯一的不同是一个字段,则添加模型污染没有没有意义.更糟糕的是,您必须修改Laravel处理关系的方式(一对多),以便在通过其他模型吸引用户时智能地返回Administrator或User对象.

This is a good idea in principle, a bad idea in practice. If both of your models use the same table and the only difference is a field, there is no point in adding model pollution. Worse still, you'd have to modify the way Laravel handles relationships (one-to-many) to intelligently return either an Administrator or User object when getting users through other models.

请考虑改为执行以下操作:

Consider doing the following instead:

 class User extends \Laravel\Eloquent {
     public function isAdministrator() { return !!$this->is_admin; }
     public static function findAdministrator($r=false) {
        if ($r) return self::where("is_admin","=",true)->where("id","=",(int)$r);
        else return self::where("is_admin","=",true);
     }
 }

执行此操作将在模型上打开两个新方法:isAdministrator,如果用户是admin,则返回boolean true,否则返回boolean false. findAdministrator,其行为与find相似,但有选择地选择管理员.

Doing this opens the two new methods on the model: isAdministrator, which returns boolean true if the user is an admin, boolean false otherwise. findAdministrator, which behaves like find but selectively picks admins.

这使您不必为本质上具有关系的两个模型(毕竟,管理员仍然是用户).它还使您可以通过有用的原子方法轻松地选择所需的内容.

This allows you to not have two models for what is essentially a relationship (an admin remains an user, after all). It also allows you to easily pick out what you need through useful, atomic methods.

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

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