额外的信息表加入没有雄辩的模型 [英] Extra information table joined without eloquent model

查看:124
本文介绍了额外的信息表加入没有雄辩的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个名为Item(名称和类型)的数据库表。现在根据类型,该项目有额外的信息:例如如果它是一个ScoredItem它有一个分数。所以我创建了一个数据库表ItemScore,它为一个项目分配一个分数。



然后我有一个雄辩的模型:Item(名称和类型),但是如何我应该去添加这个Score属性。



一个明显的解决方案是创建一个模型:ItemScore并设置正确的关系(使用hasMany和belongsTo)。但是,因为它只包含一个分数,这似乎有点过分。



所以问题是:有可能以某种方式指定这种关系,以便我可以请求得分一个项目(如果它有一个),并且设置它,以便它被正确保存在数据库中,而不创建这个模型类?

解决方案

您可以通过多态关系 / em>你将需要创建模型,以提供有关关系的雄辩的必要信息。如你所说,假设你有项目,可以有额外的信息,如得分或任何其他,让我们吧可以使用 SubItem



您必须定义这些模型,如下所示:

  class Item扩展Eloquent 
{
public function extrainfo()
{
return $ this-> morphTo();
}
}

类得分扩展Eloquent
{
public function items()
{
return $ this-> ; morphMany('Item','extrainfo');
}
}

class SubItem extends Eloquent
{
public function items()
{
return $ this-> ; morphMany('Item','extrainfo');
}
}

那么你将有一个数据库结构如下: / p>

  items 
id - integer
name - string
extrainfo_id - 整数
extrainfo_type - string

分数
id - 整数
分数 - 十进制

sub_items
id - 整数
name - string

这将自动做你所需要的。当你这样做:

  $ extrainfo = $ item-> extrainfo; 

<$ c $ extrainfo $ c> Item model将返回 Score SubItem 实例,具体取决于模型类型拥有照片。这种类型在数据库表中定义。



注意:这不是过度的,你可以看到,作为提供的元信息来告诉Enloquent如何对待你的关系。


Say I have an database table called Item (with a name and a type). Now depending on the type, the item has extra information: e.g. if it is a ScoredItem it has a Score. So I created a database table ItemScore, which assigns a score to an item.

I then have an eloquent model: Item (with the name and the type), but how should I go about adding this Score property.

An obvious solution would be to create a model: ItemScore and set the correct relation (using hasMany and belongsTo). But because it would only contain a score, this seems a bit overkill.

So the question is: is it possible to somehow specify this relation so that I can request the Score of an item (if it has one) and also set it so that it is correctly saved in the database, without creating this model class?

解决方案

What you want can be done with Polymorphic Relations, but you will need to create models to provide the necessary information to Eloquent about the relations. As you said, suppose you have Item's than can have extra information such as Score or anything else, let's it can have a SubItem instead.

You will have to define those models as shown below:

class Item extends Eloquent
{
    public function extrainfo()
    {
        return $this->morphTo();
    }
}

class Score extends Eloquent
{
    public function items()
    {
        return $this->morphMany('Item', 'extrainfo');
    }
}

class SubItem extends Eloquent
{
    public function items()
    {
        return $this->morphMany('Item', 'extrainfo');
    }
}

Then you will have a database structure as follow:

items
    id             - integer
    name           - string
    extrainfo_id   - integer
    extrainfo_type - string

scores
    id    - integer
    score - decimal

sub_items
    id   - integer
    name - string

This will automagically do what you need. When you do:

$extrainfo = $item->extrainfo;

The extrainfo relation on the Item model will return either a Score or SubItem instance, depending on which type of model owns the photo. That type is defined on the database table.

Note: This is not overkill, you can see that as meta information provided to tell Eloquent how to treat your relations.

这篇关于额外的信息表加入没有雄辩的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆