Laravel eloquent 模型 - 模型扩展模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16565486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel eloquent model - model extends model
提问by Moritz Ewert
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?
是否可以用另一个模型扩展 laravel 4 eloquent 模型,假设我有一个扩展 eloquent 类的用户模型,另外还有一个扩展用户类的管理员类?
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.
如果我只是将管理员类链接到用户,我必须首先获取用户的管理员属性,然后获取管理员属性来访问管理员属性。
EDIT:
编辑:
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;
假设我让管理员没有扩展用户。我必须像这样访问例如电话号码(管理员属性),
$user = User::find(1);
$phone = $user->administrator->phone;
但是通过让管理员扩展用户,我可以像这样直接访问电话号码
$user = Administrator::find(1);
(请注意,传递给查找管理员的 ID 是相同的就像我用来获取用户的那个。通常我必须在管理员数据库中传递条目的真实 id)
$phone = $user->phone;
同时可以访问用户类的属性,例如$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
或者也许有更好的解决方案来实现这一点,或者像这样使用它没有意义,如果是这样,请随时告诉我
回答by Sébastien Renauld
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 pointin 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.
这在原则上是个好主意,但在实践中却是个坏主意。如果您的两个模型使用相同的表并且唯一的区别是一个字段,则添加模型污染没有意义。更糟糕的是,您必须修改 Laravel 处理关系(一对多)的方式,以便在通过其他模型获取用户时智能地返回管理员或用户对象。
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);
}
}
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.
这样做会在模型上打开两个新方法:isAdministrator
,如果用户是管理员,则返回 boolean true,否则返回 boolean false。findAdministrator
,其行为类似于find
但有选择地选择管理员。
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.
这允许您不必为本质上是关系的两个模型(毕竟管理员仍然是用户)。它还允许您通过有用的原子方法轻松挑选出您需要的东西。