[英]How to build Models for (possibly) polymorphic relationship with Laravel's Eloquent ORM
I have a question regarding Eloquent ORM -- in this case, specifically being used with Laravel 4. I have had no problem using it to run basic queries and relationships, but I'm recently stumped on this somewhat unique scenario/schema: 我有一个关于Eloquent ORM的问题 - 在这种情况下,特别是与Laravel 4一起使用。我使用它来运行基本查询和关系没有问题,但我最近对这个有点独特的场景/架构感到难过:
I have three tables. 我有三张桌子。 Their structures are currently this:
他们的结构目前是这样的:
post
id - int
post_type - enum(ARTICLE, QUOTE)
user_id - int
created_at - timestamp
updated_at - timestamp
post_type_article
id - int
post_id - int
title - varchar
summary - varchar
url - varchar
post_type_quote
id - int
post_id = int
author = varchar
quote = text
At the end of this, I would like to just run one query/function using Eloquent ORM and get all posts and their respective data regardless of post_type. 最后,我想使用Eloquent ORM运行一个查询/函数,并获取所有帖子及其各自的数据,而不管post_type。
I'd really like to hear some feedback on this (my relationships, what my models should be). 我真的很想听到一些关于此的反馈(我的关系,我的模型应该是什么)。 From my understanding, this is probably a polymorphic relation.
根据我的理解,这可能是一种多态关系。 Here are/were my Models, but I am new to this, and am not sure if this the right direction or not:
这是我的模特,但我是新手,我不确定这是否正确的方向:
Model: Post.php: 型号:Post.php:
<?php
class Post extends Eloquent {
public function postType()
{
return $this->morphTo();
}
}
Model: PostTypeArticle.php: 型号:PostTypeArticle.php:
<?php
class PostTypeArticle extends Eloquent {
public $timestamps = FALSE;
protected $table = 'post_type_article';
public function post()
{
return $this->morphMany('Post', 'post_type');
}
}
Model: PostTypeQuote.php: 型号:PostTypeQuote.php:
<?php
class PostTypeQuote extends Eloquent {
public $timestamps = FALSE;
protected $table = 'post_type_quote';
public function post()
{
return $this->morphMany('Post', 'post_type');
}
}
Maybe since I'm using ENUM as a foreign key I need to explicitly specify that? 也许因为我使用ENUM作为外键我需要明确指定? Anyways, hopefully you can spot my confusion and point me in the right direction.
无论如何,希望你能发现我的困惑,并指出我正确的方向。 Thanks for your help in advanced while I get the hang of this.
谢谢你的帮助,我得到了这个。
I would be brave about this one and condense the types tables into one: post_types: 我会勇敢地讨论这个并将类型表压缩成一个:post_types:
posts 帖子
id - int (auto-increment)
user_id - int (unsigned)
created_at - timestamp
updated_at - timestamp
post_types post_types
id - int (auto-increment)
type - int //or varchar
post_id - int (unsigned)
title - varchar
summary- varchar
url - varchar
author - varchar
quote - text
Make most of the fields in the post_types table nullable (your homework) 使post_types表中的大多数字段可以为空(你的作业)
Post.php post.php中
<?php
class Post extends Eloquent {
public function postType()
{
return $this->hasOne('Posttype'); //or hasMany()
}
}
Posttype.php Posttype.php
<?php
class Posttype extends Eloquent {
protected $table = 'post_types';
public function post()
{
return $this->belongsTo('Post');
}
}
Getting Results 获得结果
$posts = Post::with('postType')->get(); //eager-loading
OR 要么
$post = Post::find(1)->postType; //One post
Homework 家庭作业
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.