Laravel 框架中的继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25036593/
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
Inheritance in Laravel framework
提问by Belsen
Is possibile to use inheritance in laravel model? I explain:
是否可以在 Laravel 模型中使用继承?我解释:
is possible to exend a model, which extends eloquent class?
是否可以扩展一个模型,它扩展了 eloquent 类?
class A extends Eloquent
{
}
class B extends A
{
}
A e B are also 2 different tables and B has A_id as foreignkey and other fields. How could be the constructor of class B? is it a ragionable solution or better use hasOne relationship?
A e B 也是 2 个不同的表,B 有 A_id 作为外键和其他字段。B类的构造函数怎么可能?这是一个合理的解决方案还是更好地使用 hasOne 关系?
no every A object are also B object. Es. user and teacher
不是每个 A 对象也是 B 对象。埃斯。用户和教师
Thank you
谢谢
回答by Martin Bean
I'm having difficulty understanding your supplementary details, but in answer to the actual question: yes, it is possible to extend Eloquent models.
我很难理解您的补充细节,但在回答实际问题时:是的,可以扩展 Eloquent 模型。
<?php
class User extends \Eloquent {
protected $table = 'users';
}
class Student extends User {
protected $table = 'students';
}
Be warned though, that any methods (such as relations, scopes etc) willpropagate to the extending class. If this is not desired, then create a base class that has the minimum of what you need, and then sub-class it with your specific types, i.e. Student
, Administrator
etc.
但是请注意,任何方法(例如关系、范围等)都将传播到扩展类。如果这不是您想要的,那么创建一个具有您需要的最少数量的基类,然后使用您的特定类型对其进行子类化,即Student
,Administrator
等等。
Another approach is to use interfaces. So if you knowmodels need the same attributes but will say, have different relations; then you can create an interface to add those constraints:
另一种方法是使用接口。因此,如果您知道模型需要相同的属性,但会说具有不同的关系;然后您可以创建一个接口来添加这些约束:
<?php
interface UserInterface {
public function getName();
}
class User extends \Eloquent implements UserInterface {
protected $table = 'users';
public function getName()
{
return $this->name;
}
}
class Student extends \Eloquent implements UserInterface {
protected $table = 'students';
public function getName()
{
return $this->name;
}
public function courses()
{
return $this->belongsToMany('Course');
}
public function grades()
{
return $this->hasMany('Grade');
}
}
回答by Belsen
I resolve with
我解决了
class A extends Eloquent
{
protected $table = 'a';
}
class B extends A
{
protected $table = 'b';
protected $primaryKey = 'a_id';
}
but in the main function:
但在主函数中:
$f = B::find(1);
$f->method();
$f = B::find(1);
$f->method();
where the method() is a method of A class,
the system gives to me an mysql error:
其中method()是A类的方法,系统给我一个mysql错误:
select * from
Cwhere
C.
B_id= 1
the error is B_id. It should be A_id cause the method shold be applied from subobject of the class, not from the class
select * from
C where
C .
B_id= 1
错误是 B_id。它应该是 A_id 因为该方法应该从类的子对象中应用,而不是从类中应用