Jas*_*onK 24 mysql inheritance subclass laravel eloquent
目前我有一个名为的模型类Post.
class Post extends Eloquent {
protected $table = 'posts';
protected $fillable = array('user_id', 'title', 'description', 'views');
/*
* Relationships
*/
public function user()
{
return $this->belongsTo('User');
}
public function tags()
{
return $this->belongsToMany('Tag', 'post_tags');
}
public function reactions()
{
return $this->hasMany('Reaction');
}
public function votes()
{
return $this->hasMany('PostVote');
}
//Scopes and functions...
}
Run Code Online (Sandbox Code Playgroud)
我想把帖子分成两种不同的类型; articles和questions.我认为最好的方法是继承,所以Article并且Question会扩展Post.最好的方法是什么,从哪里开始?
luk*_*ter 56
在深入研究多表继承之前,我想失去一些关于单表继承的话.在使用db模型进行继承时,单表继承是更简单的方法.
您有多个模型绑定到同一个表和一type列以区分不同的模型类.但是,您通常希望实现继承的原因是因为模型具有共享属性,但也有一些对模型而言是唯一的.
使用单表继承时,您的表看起来与某些点类似:
id shared_column question_column article_column question_column2 article_column2 etc...
1 Lorem 62 NULL test NULL
2 Ipsum NULL x NULL true
Run Code Online (Sandbox Code Playgroud)
最终会有很多NULL值,因为某些类型的模型不需要列.并且有很多记录,这会影响数据库的大小.
但是对于某些情况,它可能仍然是最好的解决方案.这是一本写得很好的教程,展示了如何以非常优雅的方式在Laravel中实现它.
现在让我们看一下多表继承.使用这种方法,你可以将你的单个表分成多个(我想这个名字已经提供了那种;))我们将使用一种称为多态的技术
我们上面示例中的模式如下所示:
posts table:
id shared_column postable_id postable_type
1 Lorem 1 Question
2 Ipsum 1 Article
questions table:
id question_column question_column2
1 62 test
articles table:
id article_column article_column2
1 x true
Run Code Online (Sandbox Code Playgroud)
如果你问我,那就更干净了......
这里有趣的专栏是postable_id和postable_type.该类型告诉我们在哪个表中我们将找到模型的"rest",id指定属于它的记录的主键.请注意,列名可以是您想要的任何名称,但通常称其为"-able".
让我们来看看Eloquent模型吧.
岗位
class Post extends Eloquent {
// all your existing code
public function postable(){
return $this->morphTo();
}
}
Run Code Online (Sandbox Code Playgroud)
问题/文章/其他所有类型
class Question extends Post {
public function post(){
return $this->morphOne('Post', 'postable');
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您实际上不必延伸,Post但如果您可能也有想要使用的方法,则可以.无论如何,多态关系将在有或没有它的情况下起作用.
好的,这是基本的设置.以下是使用新模型的方法:
检索所有帖子
$posts = Post::all();
Run Code Online (Sandbox Code Playgroud)
检索所有问题
$questions = Question::all();
Run Code Online (Sandbox Code Playgroud)
从帖子中获取问题列
$post = Post::find(1);
$question_column2 = $post->postable->question_column2;
Run Code Online (Sandbox Code Playgroud)
从问题中获取帖子属性
$question = Question::find(1);
$shared_column = $question->post->shared_column;
Run Code Online (Sandbox Code Playgroud)
检查帖子的类型
$post = Post::find(1);
echo 'type: '.get_class($post->postable);
if($post->postable instanceof Question){
// Looks like we got a question here
}
Run Code Online (Sandbox Code Playgroud)
创建新问题
现在忍受我,创建一个模型有点复杂.如果您必须在应用程序的多个位置执行此操作,我建议您为其编写可重用的功能.
// create a record in the questions and posts table
$question = new Question();
$question->question_column = 'test';
$question->save();
$post = new Post();
$post->shared_column = 'New Question Post';
$post->save();
// link them together
$question->post()->save($post);
Run Code Online (Sandbox Code Playgroud)
如您所见,一个干净的数据库附带它的价格.处理模型会有点复杂.但是,您可以将所有这些额外的逻辑(例如,创建模型所需的逻辑)放入模型类中的函数中,而不必过多担心它.
此外,还有一个很好的教程,用于laravel的多表继承.也许它有帮助;)
从 Laravel 5.2 开始,Global Scope可用:
class Article extends Post
{
protected $table = 'posts';
protected static function boot()
{
parent::boot();
static::addGlobalScope('article', function (Builder $builder) {
$builder->where('type', 'article');
});
static::creating(function ($article) {
$article->type = 'article'
});
}
}
Run Code Online (Sandbox Code Playgroud)
where type = 'article'被添加到Articlejust like 的所有查询中SoftDeletes。
>>> App\Article::where('id', 1)->toSql()
=> "select * from `posts` where `id` = ? and `type` = ?"
Run Code Online (Sandbox Code Playgroud)
实际上,laravelSoftDeletes使用此功能提供了trait。