doushi1929 2019-04-25 07:48
浏览 70

PHP - 保存关系 - 共享和唯一引用

I have 4 models:

  1. Stream
  2. Field
  3. Document
  4. Result

I then have below relationships defined:

Stream.php:

public function fields()
{
  return $this->hasMany(Field::class);
}
public function documents()
{
  return $this->hasMany(Document::class);
}

Field.php:

public function stream()
{
  return $this->belongsTo(Stream::class);
}

public function result()
{
  return $this->hasOne(Result::class);
}

Document.php:

public function stream()
{
  return $this->belongsTo(Stream::class);
}

Result.php:

public function field()
{
  return $this->hasOne(Field::class);
}

Now, my users can upload documents to a stream, and my users can create many fields on a stream.

When a document is uploaded to a stream,for each field defined, the document content will be parsed according to some logic I have created.

The end result of this parsing, should be saved to the database (results).

I can do this now, by:

$document = Document::find(1);

foreach($stream->fields as $field)
{
    $content = myParseFunction($document->content);

    $field->save(['content' => $content]);

}

This will create a result for each field on the stream. Like, if my Stream had 2 fields:

results table:

id | field_id | content 
1  | 1        | Lorem ipsum
2  | 2        | Another ipsum

Whenever I upload a new document however, the result will be overwritten.

As each uploaded documents content is unique, how can I save the result for the field and specific document.

Like, if my Stream still have 2 fields, but I upload 2 documents:

results table:

id | field_id | document_id | content 
1  | 1        | 1           | Lorem ipsum
2  | 2        | 1           | Another ipsum
3  | 1        | 2           | Another unique content
4  | 2        | 2           | Yet another unique content
  • 写回答

1条回答 默认 最新

  • doufangzhang4454 2019-04-25 11:33
    关注

    I think the result is also determined by the document, as you are parsing a document for a for a field. I would also link the documents to theuse the Result table ans store the results there. One result would be determinded by the document id and the field id.

    Result

    public function fields()
    {
      return $this->hasMany(Field::class);
    }
    
    public function documents()
    {
      return $this->hasMany(Document::class);
    }
    
    

    Document

    public function results()
    {
      return $this->hasMany(Result::class);
    }
    

    Field

    
    public function stream()
    {
      return $this->belongsTo(Stream::class);
    }
    
    public function result()
    {
      return $this->hasOne(Result::class);
    }
    

    And then:

    $document = Document::find(1);
    $result = new Result();
    
    foreach($stream->fields as $field)
    {
        $content = myParseFunction($document->content);
    
        $result->save(['field_id' => $field->id, 'document_id'=>$document->id, 'content' => $content]);
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)