如何使用Laravel的Eloquent ORM构建(可能)多态关系的模型

时间:2022-10-15 23:02:15

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? 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.

也许因为我使用ENUM作为外键我需要明确指定吗?无论如何,希望你能发现我的困惑,并指出我正确的方向。感谢您的帮助,同时我掌握了这一点。

1 个解决方案

#1


0  

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

家庭作业

  1. Use validation to ensure the fields you need in the database are all there in user input
  2. 使用验证来确保数据库中所需的字段都在用户输入中
  3. Decide where to run your if statements to determine whether you are dealing with an article or quote
  4. 确定运行if语句的位置,以确定您是在处理文章或引用

#1


0  

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

家庭作业

  1. Use validation to ensure the fields you need in the database are all there in user input
  2. 使用验证来确保数据库中所需的字段都在用户输入中
  3. Decide where to run your if statements to determine whether you are dealing with an article or quote
  4. 确定运行if语句的位置,以确定您是在处理文章或引用