Laravel 5-定义关系 [英] Laravel 5 - defining relationships

查看:75
本文介绍了Laravel 5-定义关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用单个模型,一切都可以正常工作,但是现在我实现了更多,发现了一个问题.

everything was working fine with a single Model, but now I am implementing more, I have noticed an issue.

我有几个代表不同类型文档的文档模型.现在,假设我有DocumentA和DocumentB.

I have several document Models which represent a different type of document. For now, let's say I have DocumentA and DocumentB.

每个文档都允许文件上传,因此我创建了一个FileUpload模型.一个文档可以有许多文件上传.

Each Document allows file uploads, so I have created a FileUpload Model. A Document can have many FileUploads.

因此,在这一点上似乎很简单.我的FileUpload表具有documentId字段,该字段是对正在使用它的Document的id字段的引用.

So, seems pretty straight forward at this point. My FileUpload table has a documentId field, which is a reference to the id field of the Document that is using it.

在DocumentA中,我有类似的东西

In DocumentA, I have something like so

public function uploadFile()
{
    return $this->hasMany('App\UploadFile', 'documentId');
}

因此DocumentA可以有许多由documentId链接的UploadFiles.

So DocumentA can have many UploadFiles, linked by the documentId.

DocumentB在其模型内具有相同的功能.

DocumentB has the same function within its Model.

我的问题在于UploadFiles模型.首先,此模型现在具有两个belongTo事件,例如

My problem lies with the UploadFiles model. Firstly, this model now has two belongTo events e.g.

public function documentA()
{
    return $this->belongsTo('App\DocumentA', 'documentId');
}

public function documentB()
{
    return $this->belongsTo('App\DocumentB', 'documentId');
}

这可能是问题,不确定我是否可以拥有多个属于?但是,我眼前的问题与doc_file表的迁移有关.目前,我有这个

This could be the problem, not sure if I can have multiple belongs to? My immediate problem however is to do with the migration of the doc_file table. At the moment I have this

Schema::table('doc_file', function (Blueprint $table) {
    $table->integer('documentId')->unsigned()->default(0);
    $table->foreign('documentId')->references('id')->on('document_a')->onDelete('cascade');
});

Schema::table('doc_file', function (Blueprint $table) {
    $table->integer('documentId')->unsigned()->default(0);
    $table->foreign('documentId')->references('id')->on('document_b')->onDelete('cascade');
});

因此,我正在尝试为文档提供外键.当我尝试迁移时,它告诉我

So I am trying to provide foreign keys to my documents. When I try to migrate, it tells me that

列已存在:1060列名称documentId重复

Column already exists: 1060 Duplicate column name documentId

我正确处理我的人际关系吗?本质上,我有很多文档,每个文档可以有很多文件.

Am I handling my relationships correctly? Essentially, I have many documents, and each document can have many files.

感谢我对数据库关系的任何帮助.

Any assistance with my database relationships appreciated.

非常感谢

推荐答案

乍一看您的问题,看来您对概念模型有点困惑.

Looking at your problem at first glance, it seems that there is a little confusion for you regarding the concept model.

模型实际上是现实世界对象的概念化,因为它用于表示现实世界实体.

A model is in fact a conceptualization of a real-world object as it is used to represent a real-world entity.

换句话说,它代表具有相似属性的一整类对象.例如,汽车模型将代表所有汽车,无论它们是 Lamborghini 还是 Mercedez 类型.事实是,它们全部属于 Car 分类.

In other words, it represents a whole class of objects with similar properties. For instance a Car model would represent all cars, whether they are of type Lamborghini or Mercedez. The fact is that they all come under the Car classification.

Eloquent和您的用例中都有相同的概念;因此, Document 模型足以代表您的两个文档(DocumentA和DocumentB).

Same concept goes in Eloquent, and with your use case; therefore a Document model is sufficient to represent both of your documents (DocumentA and DocumentB).

要完善您到目前为止所取得的成就,可以将模型的关系重构为:

To refine what you've achieved so far, your models' relationships can be refactored as such:

文档模型

public function fileUploads(){
    return $this->hasMany('App\FileUpload');
}

文件上传模型

public function document(){ 
    return $this->belongsTo('App\Document');
}

基于关系每个文档都有很多文件上传" ,以及与之相反的每个文档都有BELONGS上传文件" ,如您所见,在FileUpload模型中只有一个 belongsTo()方法来定义关系的后半部分.

Based on the relationship "EACH document has MANY file uploads", and the inverse "EACH file upload BELONGS to exactly one document", as you can see, there is only one belongsTo() method in the FileUpload model to define the latter part of the relationship.

类似地,定义上述关系的表的架构如下:

Similarly, the schema for the tables defining the above relationship are as follows:

// Schema for Document table
Schema::table('document', function (Blueprint $table) { 
    $table->increment('id'); 
    $table->string('name', 100);
});

// Schema for FileUpload table
Schema::table('doc_file', function (Blueprint $table) { // file_uploads would have been a more friendly name in my opinion
    $table->integer('documentId')->unsigned()->default(0);   // note that `documentId` is interpreted as `documentid` in MySQL
    $table->foreign('documentId')->references('id')->on('document')->onDelete('cascade');
});

这篇关于Laravel 5-定义关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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