PHP/Laravel - 共享关系的独特结果

标签 php laravel eloquent

我正在尝试创建一个网络应用程序,用户可以在其中:

  1. 上传文件
  2. 上传电子邮件(入站电子邮件)

文档/电子邮件将被上传到我称之为“流”的地方。因此一个流可以存储很多文档和很多电子邮件。

但是,我进一步尝试这样做,以便我的用户可以根据一组解析规则解析文档和电子邮件的内容。用户可以创建可以有很多解析规则的字段

示例: 用户 A 将新文档上传到名为“我的文档和电子邮件”的流中。用户为此流定义了以下两个文档字段:

  1. 订单引用
  2. 追踪号码

此外,还定义了一些电子邮件字段但不会在这种情况下使用,因为用户 A 当前正在上传新文档:

  1. 来自姓名
  2. 电子邮件主题

上面的所有字段也会有一些解析规则,这些规则将解析内容(这个例子没有显示,因为它不相关)。

如您所见,“我的文档和电子邮件”流都有 DocumentFieldsEmailFields

这是我当前的设置:

Stream.php

//A stream can have many document fields
public function documentfields()
{
    return $this->hasMany(DocumentField::class);
}
//A stream can have many email fields
public function emailfields()
{
    return $this->hasMany(EmailField::class);
}

Document.php(同样的关系在Email.php中可用)

//A document belongs to a Stream.
public function stream()
{
   return $this->belongsTo(Stream::class);
}

//Get the document fields available for the specific stream.
public function fields()
{
   return $this->stream->documentfields();
}

现在我的问题是,我该怎么做才能将字段“绑定(bind)”到流,但包含上传的每个 DocumentEmail 的唯一值?

类似于:

文档字段:

id | stream_id          | name             
1  | 1                  | Order Reference 
2  | 1                  | Tracking Number 

document_field_results

id | document_field_id  | document_id  | content
1  | 1                  | 10           | Lorem Ipsum Dolar Amet for document #10    
2  | 2                  | 10           | Some other Lorem Ipsum Dolar Amet for document #10
3  | 1                  | 55           | Lorem Ipsum Dolar Amet for document #55       
4  | 2                  | 55           | Some other Lorem Ipsum Dolar Amet for document #55      

(上传电子邮件/入站电子邮件时的逻辑相同)

所以每当一个新文档上传到流(id.1)时,它会“继承”上面的文档字段,但是每个字段的内容都是唯一的(因为每个上传的文档/email 最终会有不同的内容)。

在这种情况下,正确的关系设置应该是什么样的?这是正确的方法吗?

最佳答案

恕我直言。

流:id, name

file_categories: id, name.值:文档和电子邮件。这使您可以灵活地添加新类型的文件上传

上传:id, file_category_id, stream_id ,name

upload_fields: id, stream_id, name

upload_upload_fields: id, upload_id, upload_field_id, content

使用此设置,您无需为电子邮件和文档制作模型。此外,您可以轻松添加新的上传文件类别。

顺便说一句,我已经编程了一段时间了,但才刚刚开始回答 SO 问题。我不知道如何使数据库结构看起来像您的问题中那样漂亮。你能给我一个那个格式的链接吗?

实现

我不确定前端是什么样子的。所以我只是按照我的解释

流.php:

public function uploadFields(){
    return $this->hasMany(UploadField::class);
}

新文件上传

//First, get the uploadFields based on the stream_id
$uploadFields = Stream::with('uploadFields')->get();

//Second, insert the upload
$upload = new Upload(bla bla bla...); 
$upload->save();
//https://laravel.com/docs/5.8/eloquent#inserts
//Third, use the upload id to fill the upload_upload_fields, or if you prefer the result terms, that's okay
foreach($uploadFields as $field){
    UploadUploadField::insert(['upload_id' => $upload->id, 'upload_file_id' => $field->id, 'content' => 'The Lorem Ipsum, I guess this is from the request']);
}

我想这就是我要做的。它会有点不同,尤其是对于 content,因为它高度依赖于 request 结构。

关于PHP/Laravel - 共享关系的独特结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55775209/

相关文章:

laravel - Vue-router:如果用户没有权限,则重定向到路由

php - 拉维尔 4.2 : Copying database records from one database to another

database - 使用对象过滤查询集

Javascript 函数仅适用于表格的第一行

php - UTF-8、PHP 和 XML Mysql

php - Laravel Eloquent 的条件 wherehas

php - laravel 查询 - 调用未定义的属性 - 集合

php - 我的 Android 无法连接到我的网络服务器

php - 如何在 Zend Framework 2 中向表单添加错误消息?

javascript - 如何修复 "BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default"错误?